0

我需要编写Textprocedure (Table_txt varchar(200))从表 My_table 中扫描数据的程序例如:我有表

My_table (Id  int,  Name varchar(200))

我需要像输出文本(脚本)一样的过程执行结果

DELETE FROM My_table 
INSERT INTO My_table (Id, Name) values (1, 'Tropico') 
INSERT INTO My_table (Id, Name) values (2, 'Bus') 
INSERT INTO My_table (Id, Name) values (4, 'Africa') 
INSERT INTO My_table (Id, Name) values (8, 'Arrival') 

我可以通过程序来做吗?我愿意

CREATE TABLE My_table (Id  int,  Name varchar(200))

DELETE FROM My_table 
INSERT INTO My_table (Id, Name) values (1, 'Tropico') 
INSERT INTO My_table (Id, Name) values (2, 'Bus') 
INSERT INTO My_table (Id, Name) values (4, 'Africa') 
INSERT INTO My_table (Id, Name) values (8, 'Arrival')

ALTER PROCEDURE Textprocedure (@Table_txt varchar(200))
AS
BEGIN
DECLARE @Id1 NVARCHAR(40)
DECLARE @Result NVARCHAR(4000)
SET @Result = ''
SELECT @Result = 'INSERT INTO '+ CAST(@Table_txt AS NVARCHAR(250)) + --@Result 
+'() values ' + CAST([name] AS NVARCHAR(250)) + ' ' FROM My_table --where Id<5
PRINT (@Result)
END
GO

EXEC Textprocedure My_table

我的输出:INSERT INTO My_table() values Arrival 但我需要多行的结果。我可以骑自行车吗?我需要打印表参数,如 id、name?但是我该如何挑选它们呢?

4

2 回答 2

0

检查此项以生成插入语句:

https://stackoverflow.com/a/982576/590741

http://vyaskn.tripod.com/code/generate_inserts.txt

于 2013-10-16T07:47:21.063 回答
0

您可以使用带参数的动态 sql,然后打印或返回结果。

于 2013-10-16T08:02:21.590 回答