帮助动态Sql。 [英] Help with dynamic Sql.

查看:51
本文介绍了帮助动态Sql。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我有点卡住了,周五大脑的严重剂量:),无论如何我需要创建一个动态的sql语句而且我有点卡住。

Hey Guys, im kinda stuck at the minute, serious dose of friday brain:), Anyway I need to create a dynamic sql statement and im kinda stuck.

Declare  @string VARCHAR(MAX) = 'Test,Test,Test',
@delimiter CHAR(1) = ',',
@Sql NVARCHAR(MAX)

SET @Sql = 'Select DestinationId from dbo.Destination where '

    DECLARE @start INT, @end INT
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)

        WHILE @start < LEN(@string) + 1 BEGIN
        
        IF @end = 0 
            SET @end = LEN(@string) + 1
			
			SET @Sql = @Sql + ' DestinationName like ''' + @string + '%' + ''
        
               
        SET @start = @end + 1
        SET @end = CHARINDEX(@delimiter, @string, @start)
    END

PRINT @Sql





我希望结果查询从dbo.Destination中选择DestinationId,其中DestinationName类似于''Test%''

和DestinationName类似''Test%''

和DestinationName类似''测试%''



任何帮助都非常苛刻。



谢谢



I want the resulting query to say Select DestinationId from dbo.Destination where DestinationName like ''Test%''
And DestinationName like ''Test%''
And DestinationName like ''Test%''

Any help is very much apreciated.

Thanks

推荐答案

不是很优雅但是有效...

Not very elegant but it works ...
Declare  @string VARCHAR(MAX)
set @string = 'Test1,Test2,Test3'
declare @delimiter CHAR(1)
set @delimiter = ','
declare @Sql NVARCHAR(MAX)

declare @remstring varchar(MAX)

SET @Sql = 'Select DestinationId from dbo.Destination where '

    DECLARE @start INT, @end INT
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)

    WHILE LEN(@string) > 0 and @end > 0
    BEGIN
        SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
        if @end > 0  BEGIN
            SET @remstring = SUBSTRING(@string, 1, @end - 1)
        END
        else BEGIN
            SET @remstring = @string
        END

        SET @Sql = @Sql + 'DestinationName like ''' + @remstring + '%' + ''' and '
        set @string = substring(@string, @end + 1, len(@string) )

    END
SET @Sql = SUBSTRING(@Sql, 1, len(@Sql) - 5) -- remove the last and
PRINT @Sql


- First Crate a function



创建FU NCTION [dbo]。[Split](@ String varchar(MAX),@ Delimiter char(1))

返回@temptable TABLE(items varchar(MAX))

as

开始

声明@idx int



声明@slice varchar(8000)



选择@idx = 1

如果len(@String)< 1或@String为null返回



而@idx!= 0

开始

设置@idx = charindex(@Delimiter,@ String)

如果@ idx!= 0

set @slice = left(@ String,@ idx - 1)

else

set @slice = @String



如果(len(@slice)> 0)

开始

插入@temptable(Items)值(@slice)



结束

设置@String = right(@ String,len(@String) - @idx)

if len(@String)= 0 break

end

return

end;





- 这是查询(最后你将在名为@Sql的变量中获得所需的查询)

声明@string VARCHAR(MAX)=''测试,测试,测试',

@delimiter CHAR(1)='','',

@Sql NVARCHAR(MAX)

声明@parameter nvarchar(max)

创建表#splittedValues(QParameter nvarchar(max))



INSERT INTO #splittedValues(QParameter)

SELECT项目

来自Split(@string,'','')



select * from #splittedValues



DECLARE splitcursor CURSOR READ_ONLY

FOR SELECT QParameter

FROM #splittedValues

OPEN splitcursor

SET @Sql =''从dbo.Destination中选择DestinationId,其中''

FETCH NEXT FROM splitcursor

INTO @parameter

WHILE @@ FETCH_STATUS = 0

BEGIN



SET @Sql = @Sql +''DestinationName like''' ''+ @parameter +''%''+''''''和''

FETCH NEXT FROM splitcursor

INTO @parameter



结束



CLOSE splitcursor

DEALLOCATE splitcursor

drop table# splittedValues

SELECT SUBSTRING(@ Sql,0,len(@Sql)-3)

set @Sql = SUBSTRING(@ Sql,0,len(@Sql) - 3)

exec(@Sql) - 这里你可以执行
--First Crate a function

create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1))
returns @temptable TABLE (items varchar(MAX))
as
begin
declare @idx int

declare @slice varchar(8000)

select @idx = 1
if len(@String)<1 or @String is null return

while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String

if(len(@slice)>0)
begin
insert into @temptable(Items) values(@slice)

end
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end;


--This is the query (Finally you will get the required query in the variable named @Sql)
Declare @string VARCHAR(MAX) = ''Test,Test,Test'',
@delimiter CHAR(1) = '','',
@Sql NVARCHAR(MAX)
declare @parameter nvarchar(max)
create table #splittedValues(QParameter nvarchar(max))

INSERT INTO #splittedValues (QParameter)
SELECT items
FROM Split(@string, '','')

select *from #splittedValues

DECLARE splitcursor CURSOR READ_ONLY
FOR SELECT QParameter
FROM #splittedValues
OPEN splitcursor
SET @Sql = ''Select DestinationId from dbo.Destination where ''
FETCH NEXT FROM splitcursor
INTO @parameter
WHILE @@FETCH_STATUS = 0
BEGIN

SET @Sql = @Sql + '' DestinationName like '''''' + @parameter + ''%'' + '''''' and ''
FETCH NEXT FROM splitcursor
INTO @parameter

END

CLOSE splitcursor
DEALLOCATE splitcursor
drop table #splittedValues
SELECT SUBSTRING(@Sql,0,len(@Sql)-3)
set @Sql = SUBSTRING(@Sql,0,len(@Sql)-3)
exec(@Sql) -- Here You can execute






--First Crate a function
 
create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1))
returns @temptable TABLE (items varchar(MAX))
as
begin
declare @idx int
 
declare @slice varchar(8000)
 
select @idx = 1
if len(@String)<1 or @String is null return
 
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
 
if(len(@slice)>0)
begin
insert into @temptable(Items) values(@slice)
 
end
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end;
 

--This is the query (Finally you will get the required query in the variable named @Sql)
Declare @string VARCHAR(MAX) = ''Test,Test,Test'',
@delimiter CHAR(1) = '','',
@Sql NVARCHAR(MAX)
declare @parameter nvarchar(max)
create table #splittedValues(QParameter nvarchar(max))
 
INSERT INTO #splittedValues (QParameter)
SELECT items
FROM Split(@string, '','')

select *from #splittedValues

DECLARE splitcursor CURSOR READ_ONLY
FOR SELECT QParameter
FROM #splittedValues
OPEN splitcursor
SET @Sql = ''Select DestinationId from dbo.Destination where ''
FETCH NEXT FROM splitcursor
INTO @parameter
WHILE @@FETCH_STATUS = 0
BEGIN
 
SET @Sql = @Sql + '' DestinationName like '''''' + @parameter + ''%'' + '''''' and ''
FETCH NEXT FROM splitcursor
INTO @parameter
 
END
 
CLOSE splitcursor
DEALLOCATE splitcursor
drop table #splittedValues
SELECT SUBSTRING(@Sql,0,len(@Sql)-3)
set @Sql = SUBSTRING(@Sql,0,len(@Sql)-3)
exec(@Sql) -- Here You can execute


这篇关于帮助动态Sql。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆