0

The first time I use my scalar variable @ZeroVATRateId, inserting it into TaxRates, it works, but then on the very next line, when I insert into TaxBreakdown, it fails with:

"Must declare the scalar variable "@ZeroVATRateId".

What on earth is going on?

DECLARE @ZeroVATRateId int;

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[VatRates]') AND type in (N'U'))
BEGIN
    SET @ZeroVATRateId = 2;
END
ELSE
BEGIN
    SET @ZeroVATRateId = COALESCE((SELECT TOP 1 Id FROM [VatRates] WHERE VatBandCode = 'Zero'), 2);
END

SET IDENTITY_INSERT [dbo].[TaxRates] ON;
INSERT INTO [dbo].[TaxRates]([Id], [TaxBandCode], [Rate], [StartDate], [EndDate], [Country], [County], [City])
SELECT @ZeroVATRateId, N'ZERORATE', 1, '1900 00:00:00.000 +00:00', '99990101 00:00:00.000 +00:00', NULL, NULL, NULL
GO
SET IDENTITY_INSERT [dbo].[TaxRates] OFF;

INSERT INTO [dbo].[TaxBreakdown]([Id], [TaxRateId], [Rate], [Name], [Priority], [ApplyOnSubtotal])
SELECT @ZeroVATRateId, @ZeroVATRateId, 1, N'ZERORATE', 1, 0
GO
4

1 回答 1

4

删除该GO语句以继续使用您的变量。

于 2013-11-07T13:34:03.460 回答