我有一个查询,用于将部门插入department
表。但是,Address
表中有一个名为Address_ID
的外键。每当我想插入一个新部门时,我也想检查地址是否存在,如果不存在,我想创建一个新部门。如果确实存在,我想找到正确的ID并将其存储在变量中供以后使用。此变量已在代码的开头声明为BIGINT
。
-- If the address already exists, we will select its ID and store it in a variable.
BEGIN
SET @Address_ID =
(
SELECT TOP 1 [Address_ID]
FROM [Address]
WHERE
[Building] = @Building_Param
AND [Factory] = @Factory_Param
)
END
上面的代码是更大声明的一部分。您可以在下面找到完整的代码。
此段代码失败,并显示以下错误消息:
Die @ Address_ID-Tabellenvariable muss deklariert werden。
翻译:
必须声明@Address_ID表变量。
这是完整的代码:
/**
* Inserts a new entry into the department table.
*
* @author Rubbel Die Katz
*/
DECLARE @The_Date DATETIME2
SET @The_Date = GETDATE()
DECLARE @Department_Name_Param NVARCHAR(MAX)
SET @Department_Name_Param = ?
DECLARE @Department_Description_Param NVARCHAR(MAX)
SET @Department_Description_Param = ?
DECLARE @Factory_Param NVARCHAR(MAX)
SET @Factory_Param = ?
DECLARE @Building_Param NVARCHAR(MAX)
SET @Building_Param = ?
DECLARE @Address_ID BIGINT
SET @Address_ID = 0
-- If the address does not exist, we will need to create it automagically.
IF NOT EXISTS (
SELECT *
FROM [Address]
WHERE
[Building] = @Building_Param
AND [Factory] = @Factory_Param
)
BEGIN
INSERT INTO [Department] (
[Department_Name],
[Department_Description],
[Time_Created],
[Time_Modified]
)
OUTPUT INSERTED.[Address_ID] INTO @Address_ID
VALUES (
@Department_Name_Param,
@Department_Description_Param,
@The_Date,
@The_Date
)
END
ELSE
-- If the address already exists, we will select its ID and store it in a variable.
BEGIN
SET @Address_ID =
(
SELECT TOP 1 [Address_ID]
FROM [Address]
WHERE
[Building] = @Building_Param
AND [Factory] = @Factory_Param
)
END
-- If the department does not exist, we will create it using the Address_ID provided by our previous operations.
IF NOT EXISTS (SELECT * FROM [Department] WHERE [Department_Name] = @Department_Name_Param)
BEGIN
INSERT INTO [Department] (
[Department_Name],
[Department_Description],
[Address_ID],
[Time_Created],
[Time_Modified]
)
VALUES (
@Department_Name_Param,
@Department_Description_Param,
@Address_ID,
@The_Date,
@The_Date
)
END
我正在使用的表格都是空的。
如何从表中BIGINT
获取BIGINT
,而不是表变量?
答案 0 :(得分:1)
错误很明显,此处您尚未声明@Address_ID
。在批处理开始时,您需要DECLARE
:
DECLARE @Address_ID TABLE (AddressID BIGINT);
然后,您可以使用以下方法将该值转换为数据类型BIGINT
的变量:
DECLARE @Address_ID_BI BIGINT;
SELECT @Address_ID_BI = AddressID
FROM @Address_ID;
此 ASSUMES 您只插入一个值。
答案 1 :(得分:1)
您收到错误的原因是您没有插入地址表并获取该标识值。
如果您更改代码段:
INSERT INTO [Department] (
[Department_Name],
[Department_Description],
[Time_Created],
[Time_Modified]
)
OUTPUT INSERTED.[Address_ID] INTO @Address_ID
VALUES (
@Department_Name_Param,
@Department_Description_Param,
@The_Date,
@The_Date
)
成为:
insert into [Address] ( Building, Factory ) values ( @Building_Param, @Factory_Param )
set @Address_ID = ( SELECT SCOPE_IDENTITY() )
然后你将拥有插入的地址ID。
以下是易于测试的完整代码:
/**
* Inserts a new entry into the department table.
*
* @author Rubbel Die Katz
*/
DECLARE @The_Date DATETIME2
SET @The_Date = GETDATE()
DECLARE @Department_Name_Param NVARCHAR(MAX)
SET @Department_Name_Param = ?
DECLARE @Department_Description_Param NVARCHAR(MAX)
SET @Department_Description_Param = ?
DECLARE @Factory_Param NVARCHAR(MAX)
SET @Factory_Param = ?
DECLARE @Building_Param NVARCHAR(MAX)
SET @Building_Param = ?
DECLARE @Address_ID BIGINT
SET @Address_ID = 0
-- If the address does not exist, we will need to create it automagically.
IF NOT EXISTS (
SELECT *
FROM [Address]
WHERE
[Building] = @Building_Param
AND [Factory] = @Factory_Param
)
BEGIN
insert into [Address] ( Building, Factory ) values ( @Building_Param, @Factory_Param )
set @Address_ID = ( SELECT SCOPE_IDENTITY() )
END
ELSE
-- If the address already exists, we will select its ID and store it in a variable.
BEGIN
SET @Address_ID =
(
SELECT TOP 1 [Address_ID]
FROM [Address]
WHERE
[Building] = @Building_Param
AND [Factory] = @Factory_Param
)
END
-- If the department does not exist, we will create it using the Address_ID provided by our previous operations.
IF NOT EXISTS (SELECT * FROM [Department] WHERE [Department_Name] = @Department_Name_Param)
BEGIN
INSERT INTO [Department] (
[Department_Name],
[Department_Description],
[Address_ID],
[Time_Created],
[Time_Modified]
)
VALUES (
@Department_Name_Param,
@Department_Description_Param,
@Address_ID,
@The_Date,
@The_Date
)
END
答案 2 :(得分:1)
我在您的脚本中做了一些小改动。试试这个:
DECLARE @The_Date DATETIME2
SET @The_Date = GETDATE()
DECLARE @Department_Name_Param NVARCHAR(MAX)
SET @Department_Name_Param = ?
DECLARE @Department_Description_Param NVARCHAR(MAX)
SET @Department_Description_Param = ?
DECLARE @Factory_Param NVARCHAR(MAX)
SET @Factory_Param = ?
DECLARE @Building_Param NVARCHAR(MAX)
SET @Building_Param = ?
DECLARE @Address_ID BIGINT
SET @Address_ID = 0
-- If the address does not exist, we will need to create it automagically.
IF NOT EXISTS(
SELECT 1
FROM [Address]
WHERE [Building] = @Building_Param
AND [Factory] = @Factory_Param
)
BEGIN
INSERT INTO [Address]
(
[Factory],
[Building],
[Time_Created],
[Time_Modified]
)
VALUES
(
@Factory_Param,
@Building_Param_Param,
@The_Date,
@The_Date
)
END
--Else Part is Not Neccessary
SELECT @Address_ID = [Address_ID]
--Top 1 is not needed since the variable can hold only 1 value it will take only the Top 1 by Default
FROM [Address]
WHERE
[Building] = @Building_Param
AND [Factory] = @Factory_Param;
-- If the department does not exist, we will create it using the Address_ID provided by our previous operations.
IF NOT EXISTS(SELECT 1
FROM [Department]
WHERE [Department_Name] = @Department_Name_Param)
BEGIN
INSERT INTO [Department]
(
[Department_Name],
[Department_Description],
[Address_ID],
[Time_Created],
[Time_Modified]
)
VALUES
(
@Department_Name_Param,
@Department_Description_Param,
@Address_ID,
@The_Date,
@The_Date
)
END