Добро пожаловать в форум, Guest >> Войти | Регистрация | Поиск | Правила | | В избранное | Подписаться | ||
Все форумы / Microsoft SQL Server |
![]() ![]() |
philips Member Откуда: Сообщений: 365 |
Как из строки с разделителем получить таблицу я знаю. А как сделать обратный процесс. Т.е. - из RS2008R2 получаю в процедуру строку с разделителем ",". Получаю в процедуре таблицу (содержащую строки условий выборки ). Теперь надо получить из этой таблицы строку с разделителем and и подставить строку в exec (@string) Возможно это уже обсуждалось. |
21 авг 12, 14:22 [13043281] Ответить | Цитировать Сообщить модератору |
tpg Member Откуда: Novosibirsk Сообщений: 23902 |
??? https://www.sql.ru/faq/faq_topic.aspx?fid=731 |
21 авг 12, 14:33 [13043417] Ответить | Цитировать Сообщить модератору |
philips Member Откуда: Сообщений: 365 |
Спасибо |
21 авг 12, 14:40 [13043482] Ответить | Цитировать Сообщить модератору |
philips Member Откуда: Сообщений: 365 |
Мне нужна одна строка CREATE TABLE [dbo].[DefinedFilter]( [ID] [int] NOT NULL, [name] [varchar](50) NULL, [Value] [varchar](max) NULL ) ON [PRIMARY] ID value 1 Cast(category as int) between 1 and 7 2 categ2=1 and cast(cex as int) <12 3 (Cast(category as int) between 1 and 7 ) and cast(cex as int) <12 4 prof >200000000 and (Cast(category as int) between 1 and 7 ) 5 prof >200000000 and (Cast(category as int) between 1 and 7 ) and groupdep=30 6 (Cast(category as int) between 1 and 7 ) and cast(cex as int) <81 and prof <200000000 Делаю ; with test (id,value) as (select 1 as id,value from [dbo].[DefinedFilter]) select top 1 [Описание] = replace((select [value] + ' and ' from test where id = t.id for xml path('')), ' ', ' ') from test t Получаю Cast(category as int) between 1 and 7 and categ2=1 and cast(cex as int) <12 and (Cast(category as int) between 1 and 7 ) and cast(cex as int) <12 and prof >200000000 and (Cast(category as int) between 1 and 7 ) and prof >200000000 and (Cast(category as int) between 1 and 7 ) and groupdep=30 and (Cast(category as int) between 1 and 7 ) and cast(cex as int) <81 and prof <200000000 and А как заменить &; на нормальные операнды что то писали select '&'+'<'+'>'+'='+CHAR(0)+CHAR(1) for xml path('') но я не понял куда это вставить |
21 авг 12, 16:06 [13044251] Ответить | Цитировать Сообщить модератору |
Knyazev Alexey Member Откуда: Екб -> Мск Сообщений: 10232 Блог |
select ( select '&'+'<'+'>'+'=' for xml path(''), type ).value( '.', 'nvarchar(100)' ) |
21 авг 12, 16:48 [13044692] Ответить | Цитировать Сообщить модератору |
nezhadnye_my
Guest |
; with test (id,value) as (select 1, 'Cast(category as int) between 1 and 7' union all select 2, 'categ2=1 and cast(cex as int) <12' union all select 3, '(Cast(category as int) between 1 and 7 ) and cast(cex as int) <12' union all select 4, 'prof >200000000 and (Cast(category as int) between 1 and 7 )' union all select 5, 'prof >200000000 and (Cast(category as int) between 1 and 7 ) and groupdep=30' union all select 6, '(Cast(category as int) between 1 and 7 ) and cast(cex as int) <81 and prof <200000000' ) select top 1 [Описание] = stuff((select ' and ' + [value] from test FOR XML PATH(''), TYPE).value('.','nvarchar(max)'), 1, 5, '') from test t Cast(category as int) between 1 and 7 and categ2=1 and cast(cex as int) <12 and (Cast(category as int) between 1 and 7 ) and cast(cex as int) <12 and prof >200000000 and (Cast(category as int) between 1 and 7 ) and prof >200000000 and (Cast(category as int) between 1 and 7 ) and groupdep=30 and (Cast(category as int) between 1 and 7 ) and cast(cex as int) <81 and prof <200000000 |
21 авг 12, 16:57 [13044764] Ответить | Цитировать Сообщить модератору |
philips Member Откуда: Сообщений: 365 |
Круто, ничего не понял, но классно. Спасибо |
21 авг 12, 17:00 [13044792] Ответить | Цитировать Сообщить модератору |
nezhadnye_my
Guest |
for xml path('') собирает столбец в строку, но при этом спецсимволы XML, имеющиеся в строках данных, заменятся: & -> & < -> < > -> > " -> " ' -> ' Knyazev Alexey показал, как этого избежать. сравните: select '&'+'<'+'>'+'=' for xml path('') select ( select '&'+'<'+'>'+'=' for xml path(''), type ).value( '.', 'nvarchar(100)' ) |
21 авг 12, 17:39 [13045046] Ответить | Цитировать Сообщить модератору |
nezhadnye_my
Guest |
пардон, надо было картинкой завесить, что он на что заменяет (в сообщении все обратно и заменилось ![]() К сообщению приложен файл. Размер - 42Kb |
21 авг 12, 17:45 [13045082] Ответить | Цитировать Сообщить модератору |
Все форумы / Microsoft SQL Server | ![]() |