我遇到一个奇怪的问题,即同一脚本有不同的结果。下面是我的代码:
DesiredSize.Height
当我使用SQL Server Management Studio 2014 UI运行此程序时,当我在ExtractForStandardCustomer表中插入内容时,触发器将正常执行。它使用所需的记录填充ExtractForCustomerLoyalty表...
但是,当我尝试通过cmd运行相同的脚本时:
MaxHeight
它抛出此错误:
--Create table ExtractForCustomerLoyalty--------------------------------------------------------------------
--This contains the customer extract records and is used by CustomerLoyaltyExporter batch job.
--Version 1 - 8/14/2018
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'ExtractForCustomerLoyalty')
BEGIN
Print 'Creating [dbo].[ExtractForCustomerLoyalty] table...'
CREATE TABLE [dbo].[ExtractForCustomerLoyalty](
[rowID] [int] NOT NULL,
[sequenceCount] [int] NOT NULL,
[extractTime] [datetime] NOT NULL,
[extractData] [varchar](8000) NOT NULL,
[exported] [varchar](1) NOT NULL DEFAULT ('N')
) ON [PRIMARY]
END
GO
--Create trigger customer_extract---------------------------------------------------------------------------
--This trigger is used to create customer extract records
--Version 1 - 8/14/2018
IF EXISTS (SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID(N'['+OBJECT_SCHEMA_NAME(object_id)+N'].[customer_extract]') AND [type] = 'TR')
BEGIN
Print 'Dropping [dbo].[customer_extract] Trigger'
DROP TRIGGER [dbo].[customer_extract];
END;
GO
CREATE TRIGGER [dbo].[customer_extract] on [CCSV4].[dbo].[ExtractForStandardCustomer]
AFTER INSERT
AS
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET ARITHABORT ON
SET CONCAT_NULL_YIELDS_NULL ON
SET QUOTED_IDENTIFIER ON
--Just in case we need to deactivate trigger
DECLARE @Cinfo VARBINARY(128)
SELECT @Cinfo = Context_Info()
IF @Cinfo = 0x55555
RETURN
--Declaration
DECLARE @rowID int
DECLARE @dataXML XML
DECLARE @data VARCHAR(MAX) = ''
-- Get the complete extractData xml from ExtractForStandardCustomer table
SET @rowID = (select rowID from inserted)
SELECT @data = @data + extractData FROM ExtractForStandardCustomer where rowID = @rowID order by sequenceCount
SET @dataXML = TRY_CAST(@data as XML)
IF @dataXML IS NULL
RETURN
-- Check the CustomerUpdate type (Create / Update / Delete)
IF (@dataXML.exist('(/CustomerExtract/Change)') = 1)
-- Customer Update
INSERT INTO [dbo].[ExtractForCustomerLoyalty](rowID, sequenceCount, extractTime, extractData)
SELECT i.rowID, i.sequenceCount, i.extractTime,
'' + '|' +
'' + '|' +
ISNULL(@dataXML.value('(//After/Customer/Loyalty/LoyaltyID)[1]', 'VARCHAR(8000)'),'') + '|' +
ISNULL(@dataXML.value('(//After/Customer/Loyalty/JoinLocation)[1]', 'VARCHAR(8000)'),'') + '|' +
ISNULL(@dataXML.value('(//After/Customer/Loyalty/JoinDate)[1]', 'VARCHAR(8000)'),'') + '|' +
ISNULL(@dataXML.value('(//After/Customer/Loyalty/FirstPurchaseDate)[1]', 'VARCHAR(8000)'),'') + '|' +
ISNULL(@dataXML.value('(//After/Customer/Loyalty/FirstPurchaseLocation)[1]', 'VARCHAR(8000)'),'') + '|'
---More here
FROM inserted i
ELSE
BEGIN
--Customer Create / Delete
INSERT INTO [dbo].[ExtractForCustomerLoyalty](rowID, sequenceCount, extractTime, extractData)
SELECT top 1 i.rowID, i.sequenceCount, i.extractTime,
'' + '|' + --CUSTOMER_ID (NULL for now)
'' + '|' + --HOUSEHOLD_ID (NULL for now)
ISNULL(@dataXML.value('(//Customer/Loyalty/LoyaltyID)[1]', 'VARCHAR(8000)'),'') + '|' +
ISNULL(@dataXML.value('(//Customer/Loyalty/JoinLocation)[1]', 'VARCHAR(8000)'),'') + '|' +
ISNULL(@dataXML.value('(//Customer/Loyalty/JoinDate)[1]', 'VARCHAR(8000)'),'') + '|' +
ISNULL(@dataXML.value('(//Customer/Loyalty/FirstPurchaseDate)[1]', 'VARCHAR(8000)'),'') + '|' +
ISNULL(@dataXML.value('(//Customer/Loyalty/FirstPurchaseLocation)[1]', 'VARCHAR(8000)'),'') + '|' +
ISNULL(@dataXML.value('(//Customer/Name/First)[1]', 'VARCHAR(8000)'),'') + '|'
--More here
FROM inserted i
END
GO
答案 0 :(得分:2)
不同的工具和库具有不同的默认设置。
连接时,SQL Server的SQL Server本机客户端ODBC驱动程序和SQL Server的SQL Server本机客户端OLE DB提供程序会自动将QUOTED_IDENTIFIER设置为ON。可以在ODBC数据源,ODBC连接属性或OLE DB连接属性中进行配置。对于来自DB-Library应用程序的连接,SET QUOTED_IDENTIFIER的默认值为OFF。
如果您不满意接受默认值,或者(如此处所示)希望无论使用什么默认值都能够可预测地运行脚本,请显式设置所需的选项。
还要特别注意QUOTED_IDENTIFIER
和ANSI_NULLS
是在定义模块(例如触发器,存储的proc等)时捕获的。您想确保将这些设置应用到您的连接上,而不使用任何特定的模块定义。
在执行-I
时始终指定SQLCMD
命令行参数是一个好习惯(也许是最好的做法),以便默认情况下QUOTED_IDENTIFIER
处于打开状态。为了向后兼容,SQLCMD
中的设置默认为关闭。