1

我需要将 text( SQL) 添加/附加到现有script.

我有一个为我STORED PROCEDURE生成INSERT statements的。它们已退出分配,因此目前我将它们复制并粘贴到所需的scripts.

这基本上是 SQL 的样子:

SELECT '' + CHAR(13) + CHAR(10) + 'EXECUTE [procSystemAppEntity_InsertOrUpdate] ' + CHAR(13) + CHAR(10) + ' @SystemUID = @SystemUID' + CHAR(13) + CHAR(10) 
+ ',@SystemAppCode = @SystemAppCode' + CHAR(13) + CHAR(10) 
+ ',@SystemAppEntityTypeCode = N''' + saet.[SystemAppEntityTypeCode] + '''' + CHAR(13) + CHAR(10) 
+ ',@SystemAppEntityUID = '''+ CAST((SELECT NEWID()) AS nvarchar(max)) + '''' + CHAR(13) + CHAR(10) 
+ ',@SystemAppEntityCode = ''' + tab.name + '''' + CHAR(13) + CHAR(10) 
+ ',@SystemAppEntityName = N''' + tab.name + '''' + CHAR(13) + CHAR(10) 
+ ',@TableName = N''' + tab.name + '''' + CHAR(13) + CHAR(10) 
+ ',@TableKeyColumnName = N''' + tab.name + 'ID''' + CHAR(13) + CHAR(10) 
+ ',@TableKeyCodeColumnName = N''' + tab.name + 'Code''' + CHAR(13) + CHAR(10) 
+ ',@ManagedAssemblyName = N''' + CASE WHEN @SystemAppEntityManagedAssemblyName IS NULL THEN '' ELSE @SystemAppEntityManagedAssemblyName END + '''' + CHAR(13) + CHAR(10) 
+ ', @ManagedNamespace = N''' + CASE WHEN @SystemAppEntityManagedNamespace IS NULL THEN '' ELSE @SystemAppEntityManagedNamespace END + '''' + CHAR(13) + CHAR(10) 
+ ', @ManagedClassName = ''' + tab.name + '''' + CHAR(13) + CHAR(10) 
+ ', @IsMultiple = ' + CAST(@IsMultiple AS varchar(1)) + CHAR(13) + CHAR(10) 
+ ', @IsCodeGenerated = 1' + CHAR(13) + CHAR(10) 
+ ', @IsActive = 1' + CHAR(13) + CHAR(10) 
+ ', @SystemAppUserID = @CreationSystemAppUserID ' + CHAR(13) + CHAR(10) + '; '
FROM sys.tables tab INNER JOIN
    [SystemAppEntityType] saet ON saet.[IsMultipleEntity] = CASE WHEN RIGHT(tab.name, 1) = 's' THEN 1 ELSE 0 END AND saet.[IsNavigationEntity] = 0 AND saet.IsSystemEntity = CASE WHEN LEFT(tab.name, 3) = 'Sys' THEN 1 ELSE 0 END
WHERE (tab.name NOT IN (SELECT e.[TableName] FROM [SystemAppEntity] e))
    AND (tab.name = @Code)

输出为:

EXECUTE [procSystemAppEntity_InsertOrUpdate] 
 @SystemUID = @SystemUID
,@SystemAppCode = @SystemAppCode
,@SystemAppEntityTypeCode = N'App-S'
,@SystemAppEntityUID = '11E347C7-7E69-4555-A6F0-2AFE142FC25F'
,@SystemAppEntityCode = 'Revision'
,@SystemAppEntityName = N'Revision'
,@TableName = N'Revision'
,@TableKeyColumnName = N'RevisionID'
,@TableKeyCodeColumnName = N'RevisionCode'
,@ManagedAssemblyName = N'SomeManagedAssemblyName '
, @ManagedNamespace = N'SomeManagedNamespace '
, @ManagedClassName = 'Revision'
, @IsMultiple = 0
, @IsCodeGenerated = 1 
, @IsIntegratedToStaging = 1
, @IsActive = 1
, @SystemAppUserID = @CreationSystemAppUserID 
;

话虽这么说,有没有一种方法可以让我Output statements直接将其写入/附加到所需的脚本?

我只是希望它添加到脚本的底部。这可能吗?如果可以,我将如何实现?

4

2 回答 2

1
SELECT '' + CHAR(13) + CHAR(10) + 'EXECUTE [procSystemAppEntity_InsertOrUpdate] ' + CHAR(13) + CHAR(10) + ' @SystemUID = @SystemUID' + CHAR(13) + CHAR(10)  
+ ',@SystemAppCode = @SystemAppCode' + CHAR(13) + CHAR(10)  
+ ',@SystemAppEntityTypeCode = N''' + saet.[SystemAppEntityTypeCode] + '''' + CHAR(13) + CHAR(10)  
+ ',@SystemAppEntityUID = '''+ CAST((SELECT NEWID()) AS nvarchar(max)) + '''' + CHAR(13) + CHAR(10)  
+ ',@SystemAppEntityCode = ''' + tab.name + '''' + CHAR(13) + CHAR(10)  
+ ',@SystemAppEntityName = N''' + tab.name + '''' + CHAR(13) + CHAR(10)  
+ ',@TableName = N''' + tab.name + '''' + CHAR(13) + CHAR(10)  
+ ',@TableKeyColumnName = N''' + tab.name + 'ID''' + CHAR(13) + CHAR(10)  
+ ',@TableKeyCodeColumnName = N''' + tab.name + 'Code''' + CHAR(13) + CHAR(10)  
+ ',@ManagedAssemblyName = N''' + CASE WHEN @SystemAppEntityManagedAssemblyName IS NULL THEN '' ELSE @SystemAppEntityManagedAssemblyName END + '''' + CHAR(13) + CHAR(10)  
+ ', @ManagedNamespace = N''' + CASE WHEN @SystemAppEntityManagedNamespace IS NULL THEN '' ELSE @SystemAppEntityManagedNamespace END + '''' + CHAR(13) + CHAR(10)  
+ ', @ManagedClassName = ''' + tab.name + '''' + CHAR(13) + CHAR(10)  
+ ', @IsMultiple = ' + CAST(@IsMultiple AS varchar(1)) + CHAR(13) + CHAR(10)  
+ ', @IsCodeGenerated = 1' + CHAR(13) + CHAR(10)  
+ ', @IsActive = 1' + CHAR(13) + CHAR(10)  
+ ', @SystemAppUserID = @CreationSystemAppUserID ' + CHAR(13) + CHAR(10) + '; 
Insert into table(...) ;' 
FROM sys.tables tab INNER JOIN 
    [SystemAppEntityType] saet ON saet.[IsMultipleEntity] = CASE WHEN RIGHT(tab.name, 1) = 's' THEN 1 ELSE 0 END AND saet.[IsNavigationEntity] = 0 AND saet.IsSystemEntity = CASE WHEN LEFT(tab.name, 3) = 'Sys' THEN 1 ELSE 0 END 
WHERE (tab.name NOT IN (SELECT e.[TableName] FROM [SystemAppEntity] e)) 
    AND (tab.name = @Code) 
于 2012-07-18T09:20:18.843 回答
0

您使用的是什么版本的 SQL Server?

如果您使用的是 SQL Server 2008+,那么您可以通过 Generate Script 实用程序本身生成 INSERT 脚本。

在 SQL Server 2005 或更早版本中(我也在 SQL 2K 中尝试过),那么有一个非常好的 stroed proc 可以为给定的表生成 INSERT 脚本。查看http://vyaskn.tripod.com/code/generate_inserts.txt

于 2012-07-18T09:21:51.387 回答