如何在SQL中的一行中声明和分配一个variables
我想要类似的东西
DECLARE myVariable nvarchar[MAX] = "hello world".
奖励点,如果你告诉我如何编码string中的报价。
例如:
我想要读取string
John said to Emily "Hey there Emily"
我的尝试将是
DECLARE myVariable nvarchar[MAX] = "John said to Emily \"Hey there Emily\""
开始:
DECLARE @var nvarchar(max) = 'Man''s best friend';
你会注意到这个'是通过把它翻倍'来逃脱'' 。
由于string分隔符是'而不是" ,所以不需要转义" :
DECLARE @var nvarchar(max) = '"My Name is Luca" is a great song';
DECLARE的MSDN页面中的第二个例子显示了正确的语法。
在SQL 2008上这是有效的
DECLARE @myVariable nvarchar(Max) = 'John said to Emily "Hey there Emily"' select @myVariable
在SQL Server 2005上,你需要这样做
DECLARE @myVariable nvarchar(Max) select @myVariable = 'John said to Emily "Hey there Emily"' select @myVariable
你几乎得到了它:
DECLARE @myVariable nvarchar(max) = 'hello world';
看到这里的文档
对于引号,SQL Server使用撇号,而不是引号:
DECLARE @myVariable nvarchar(max) = 'John said to Emily "Hey there Emily"';
如果你需要在string中使用双撇号,请使用双撇号:
DECLARE @myVariable nvarchar(max) = 'John said to Emily ''Hey there Emily''';