我正在使用 ScriptDom 来解析 SQL 脚本。我的程序如下
static void Main(string[] args)
{
string script = @"
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE dbo.ws_Device_Update
(
@ApplicationId uniqueidentifier ,
@OriginalApplicationId uniqueidentifier ,
@DeviceIMEI nvarchar (50) ,
@OriginalDeviceIMEI nvarchar (50) ,
@ModelId int ,
@DeviceName nvarchar (50) ,
@DeviceDescription nvarchar (255) ,
@DeviceState int ,
@IsExpired bit ,
@IsSuspended bit ,
@LastAccessed datetime ,
@ClientSeqNo bigint ,
@ServerSeqNo bigint ,
@ModelCode varchar (50) ,
@PushToken varchar (512) ,
@PushLastAlive datetime ,
@PushLastDead datetime ,
@DeviceType int
)
AS
UPDATE dbo.[ws_Device]
SET
[ModelId] = @ModelId --Does a device model change with same DeviceIMEI .I doubt it
,[DeviceName] = @DeviceName --Does a device name change with same DeviceIMEI .I doubt it
,[DeviceDescription] = @DeviceDescription --Does a device description change with same DeviceIMEI .I doubt it
,[DeviceState] = @DeviceState
,[IsExpired] = @IsExpired
,[IsSuspended] = @IsSuspended
,[LastAccessed] = @LastAccessed
,[ClientSeqNo] = @ClientSeqNo
,[ServerSeqNo] = @ServerSeqNo
,[ModelCode] = @ModelCode --Does a device model code with same DeviceIMEI .I doubt it
,[PushToken] = @PushToken
,[PushLastAlive] = @PushLastAlive
,[PushLastDead] = @PushLastDead
,[DeviceType] = @DeviceType --Does a device device type change with same DeviceIMEI .I doubt it
WHERE
[ApplicationId] = @OriginalApplicationId
AND [DeviceIMEI] = @OriginalDeviceIMEI
";
IList<ParseError> parseErrors;
TSql100Parser tsqlParser = new TSql100Parser(false);
TSqlFragment fragment;
using (StringReader stringReader = new StringReader(script))
{
fragment = (TSqlFragment)tsqlParser.Parse(stringReader, out parseErrors);
}
if (parseErrors.Count > 0)
{
Console.WriteLine(@"Errors encountered: ""{0}""", parseErrors[0].Message);
}
Console.ReadKey();
}
工作正常。
但如果我不提
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
它失败
遇到的错误:“CREATE 附近的语法不正确。”
我的问题是它为什么会发生......没有SET QUOTED_IDENTIFIER ON或 SET ANSI_NULLS ON,它仍然是一个有效的存储过程。
或者是否必须使用这些?有没有办法绕过它?