如何为此编写动态 SQL?

发布于 2024-11-10 11:02:02 字数 705 浏览 4 评论 0原文

我需要编写一个只有执行语句的过程。

示例:

Create Procedure dbo.allSPExecute( @id int)
as
begin

EXEC dbo.tic @day= 7,@name = 'Gname',@Type = 'Utype'
EXEC dbo.tic @day= 7,@name = 'tname',@Type = 'Utype'
EXEC dbo.gtype @day = 7,@Tname = 'UName_By',@Udept = 'Dept'

End

我有超过 50 条这样的语句。我有一个查找表:

ID  Name        SCol      Dcol       IOrd
1   dbo.tic     Gname     @name       1
1   dbo.tic     tname     @name       2
1   dbo.tic     Utype     @Type       1
1   dbo.tic     Utype     @Type       2
2   dbo.gtype   UName_By  @Tname      1
2   dbo.gtype   Dept      @Udept      1

有没有办法让如果我传递 ID,那么它将从查找表中获取值并执行该过程。有人可以帮忙吗?

编辑:更改了查找表中的数据并创建过程

I need to write a procedure which will have just execute statements.

Example:

Create Procedure dbo.allSPExecute( @id int)
as
begin

EXEC dbo.tic @day= 7,@name = 'Gname',@Type = 'Utype'
EXEC dbo.tic @day= 7,@name = 'tname',@Type = 'Utype'
EXEC dbo.gtype @day = 7,@Tname = 'UName_By',@Udept = 'Dept'

End

I have more than 50 statements like this.I have a Table which is Lookup:

ID  Name        SCol      Dcol       IOrd
1   dbo.tic     Gname     @name       1
1   dbo.tic     tname     @name       2
1   dbo.tic     Utype     @Type       1
1   dbo.tic     Utype     @Type       2
2   dbo.gtype   UName_By  @Tname      1
2   dbo.gtype   Dept      @Udept      1

Is there a way so that If i pass ID then it will take the values from the Lookup table and execute the procedure. Can anyone help?

EDIT: Changed the Data from Lookpup table and Create Procedure

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伴随着你 2024-11-17 11:02:02

最简单的解决方案 - 让你的存储过程像这样:

Create Procedure dbo.allSPExecute( @id int)
as

DECLARE @SQL varchar(max)

SET @SQL = 'USE MyDB'

SELECT @SQL = @SQL +
'EXEC ' + t.Name + ' @day = 7,' + t.Dcol + ' = ''' + t.Scol + ''
FROM MyTable t
WHERE t.id = @ID

EXEC (@SQL)

Easiest solution - make your stored proc like this:

Create Procedure dbo.allSPExecute( @id int)
as

DECLARE @SQL varchar(max)

SET @SQL = 'USE MyDB'

SELECT @SQL = @SQL +
'EXEC ' + t.Name + ' @day = 7,' + t.Dcol + ' = ''' + t.Scol + ''
FROM MyTable t
WHERE t.id = @ID

EXEC (@SQL)
云淡月浅 2024-11-17 11:02:02

这是一个不需要游标的解决方案。我使用 FOR XML 关键字将字符串连接在一起。我在两个地方执行此操作 - 一次用于连接参数列表,一次用于连接所有单独的 exec 语句。我还没有测试过这个,但我认为这应该比使用游标快得多

CREATE PROCEDURE dbo.allSPExecute( @id INT ) 
AS  
BEGIN   
  DECLARE @query VARCHAR(MAX);   
  SELECT @query =   (
    SELECT ExecPart + ' ' + SUBSTRING(Params,2,LEN(Params)) + CHAR(10)
    FROM
    (
      SELECT 
        ExecPart ='EXEC ' +(SELECT TOP 1 Name FROM MyTable WHERE ID = @id) ,
        Params = (SELECT ', ' + Dcol + ' = ' + Scol 
                  FROM MyTable t1 WHERE ID = @id AND t1.IOrd = t.IOrd FOR XML PATH(''))
      FROM MyTable t
      WHERE ID = @id
      GROUP BY IOrd
    ) t
    FOR XML PATH('')   
  ) 

  EXEC (@query) 
END

here is a solution that does not need cursors. I am using FOR XML keyword to concatenate strings together. I am doing this in two places - once for concatenating the list of parameters, and once for concatenating all the individual exec statements. I haven't tested this though, but i think this should be much faster than using cursors

CREATE PROCEDURE dbo.allSPExecute( @id INT ) 
AS  
BEGIN   
  DECLARE @query VARCHAR(MAX);   
  SELECT @query =   (
    SELECT ExecPart + ' ' + SUBSTRING(Params,2,LEN(Params)) + CHAR(10)
    FROM
    (
      SELECT 
        ExecPart ='EXEC ' +(SELECT TOP 1 Name FROM MyTable WHERE ID = @id) ,
        Params = (SELECT ', ' + Dcol + ' = ' + Scol 
                  FROM MyTable t1 WHERE ID = @id AND t1.IOrd = t.IOrd FOR XML PATH(''))
      FROM MyTable t
      WHERE ID = @id
      GROUP BY IOrd
    ) t
    FOR XML PATH('')   
  ) 

  EXEC (@query) 
END
花伊自在美 2024-11-17 11:02:02

这是一个从头开始编写且未经测试的游标示例:

declare @dynsql as nvarchar(300)
declare @tname as nvarchar(100)
declare @tscol as nvarchar(100)
declare @tdcol as nvarchar(100)

set @dynsql = ''

declare ticker as cursor for select Name, SCol, DCol from Lookup
open ticker
fetch next from ticker into @tname, @tscol, @tdcol

while @@FETCH_STATUS = 0
BEGIN

set @dynsql = 'EXEC ' + @tname + ' @day=7, ' + @tdcol + '=''' + @tscol + ''''

exec(@dynsql)

FETCH NEXT from ticker into @tname, @tscol, @tdcol
END

close ticker
deallocate ticker

Here's a cursor example written from scratch and untested:

declare @dynsql as nvarchar(300)
declare @tname as nvarchar(100)
declare @tscol as nvarchar(100)
declare @tdcol as nvarchar(100)

set @dynsql = ''

declare ticker as cursor for select Name, SCol, DCol from Lookup
open ticker
fetch next from ticker into @tname, @tscol, @tdcol

while @@FETCH_STATUS = 0
BEGIN

set @dynsql = 'EXEC ' + @tname + ' @day=7, ' + @tdcol + '=''' + @tscol + ''''

exec(@dynsql)

FETCH NEXT from ticker into @tname, @tscol, @tdcol
END

close ticker
deallocate ticker
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文