如何为此编写动态 SQL?
我需要编写一个只有执行语句的过程。
示例:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的解决方案 - 让你的存储过程像这样:
Easiest solution - make your stored proc like this:
这是一个不需要游标的解决方案。我使用 FOR XML 关键字将字符串连接在一起。我在两个地方执行此操作 - 一次用于连接参数列表,一次用于连接所有单独的 exec 语句。我还没有测试过这个,但我认为这应该比使用游标快得多
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
这是一个从头开始编写且未经测试的游标示例:
Here's a cursor example written from scratch and untested: