如何在n脚本的底部编写/附加文本?

时间:2012-07-18 09:13:35

标签: sql sql-server

我需要在现有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直接写入/追加到所需的脚本?

我只想将它添加到脚本的底部。这是可能的,如果是这样,我将如何实现这一目标?

2 个答案:

答案 0 :(得分: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) 

答案 1 :(得分: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