简体   繁体   English

适用于Exec()的T-SQL换行符字符串连接

[英]T-SQL Newline String Concatenation For Exec()

So, I've come across a pretty annoying problem with T-SQL... Essentially, in a table, there are several T-SQL statements. 因此,我遇到了T-SQL的一个非常烦人的问题...本质上,在一个表中,有几个T-SQL语句。 I want a stored procedure to efficiently grab these rows and concatenate them to a variable. 我希望存储过程可以有效地捕获这些行并将它们连接为变量。 Then, execute the concatenated lines with EXEC(@TSQL) 然后,使用EXEC(@TSQL)执行连接的行

The problem is, string concatenation with newlines seem to be stripped when calling Exec... 问题是,调用Exec时,似乎会删除带有换行符的字符串连接。

For example something like: 例如类似:

declare @sql nvarchar(max) = ''
select @sql += char(13) + char(10) + [sql] from [SqlTable]
exec(@sql) -- Won't always do the right thing
print @sql -- Looks good

I don't want to butcher the code with a Cursor, is there any way around this? 我不想用游标来宰杀代码,这有什么办法吗? Thanks! 谢谢!

Edit: Okay, so it looks like the issue is in fact only with the GO statement, for example: 编辑:好的,因此看起来该问题实际上仅与GO语句有关,例如:

declare @test nvarchar(max) = 'create table #test4(i int) ' + char(10) + char(13) + 'GO' + char(10) + char(13) +'create table #test5(i int)'
exec(@test)

I guess this go will have to go (no pun intended) I just really didn't want to have to try and parse it in fear of special cases blowing up the whole thing. 我想这将是必须要做的(没有双关语),我只是真的不想不想尝试解析它,以免担心特殊情况会炸毁整个东西。

A select statement without order by is free to return results in any order. 没有order by select语句可以按任意顺序返回结果。

You'd have to specify the order in which your SQL snippets make sense: 您必须指定您的SQL代码段有意义的顺序:

select  @sql += char(13) + char(10) + [sql] 
from    [SqlTable]
order by
        SnippetSequenceNr

As @Bort suggested in the comments, if the snippets are stand-alone SQL, you can separate them with a semicolon. 正如@Bort在注释中建议的那样,如果代码段是独立的SQL,则可以使用分号将它们分开。 Carriage returns, newlines, tabs and spaces are all the same in T-SQL: they're whitespace. 回车,换行符,制表符和空格在T-SQL中都是相同的:它们是空格。

select  @sql += [sql] + ';'
from    [SqlTable]
order by
        SnippetSequenceNr

Just get rid of the GO "statements". 只是摆脱GO “声明”。 As noted by others you also might need to ensure the string is constructing in the correct statement sequence. 正如其他人所指出的那样,您可能还需要确保字符串以正确的语句顺序构造。 Using += is probably not the best idea, though I'm not sure about the dynamic sql idea in the first place. 尽管我不确定动态sql的想法,但是使用+=可能不是最好的想法。 It might actually be more appropriate to use a cursor here. 实际上,在此处使用游标可能更合适。

Sam F, 山姆

Your method didn't work with FOR XML method of string concatenation (if you wanted to create a "line" delimited list, based on values found in different rows of a table). 您的方法不适用于字符串连接的FOR XML方法(如果要基于在表的不同行中找到的值创建“行”定界列表)。 However, replace the char(13) with SPACE(13), then it works great. 但是,将char(13)替换为SPACE(13),则效果很好。

SELECT PackageNote = SUBSTRING((SELECT (SPACE(13) + char(10) + PackageDescription)
FROM POPackageNote PN2
WHERE PN1.PurchaseOrderNumber = PN2.PurchaseOrderNumber
ORDER BY POPackageNoteID, PackageDescription
    FOR XML PATH( '' ) 
    ), 3, 1000 )
FROM POPackageNote PN1
WHERE (PurchaseOrderNumber = @PurchaseOrderNumber)
GROUP BY PurchaseOrderNumber 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM