简体   繁体   English

T-SQL-在单一存储过程中,要将一个查询的值(结果)传递给另一个查询作为输入

[英]T-SQL - In Single Stored procedure want to pass value(result) of one query to another query as input


I have a Stored procedure, in which I have to insert 3 strings into 3 different Tables at a time, each string into each of the 3 tables. 我有一个存储过程,其中我必须一次将3个字符串插入3个不同的表中,每个字符串插入3个表中的每个表中。
In each table, a unique primary key (rowid) would be generated on insertion of the value. 在每个表中,在插入值时将生成唯一的主键(行)。
Now, the Primary Key of first two tables is the Foreign key of the Third Table which as you all know, should not be null. 现在,前两个表的主键是第三个表的外键,众所周知,它不应为空。

Here in my SP, insertion of value and generation of RowID (PrimaryKey) is done successfully. 在我的SP中,成功完成了值的插入和RowID(PrimaryKey)的生成。
Now I have to pass the two primary keys(Rowids) as values/Parameters(foreignkeys) into the third table, which is returning null. 现在,我必须将两个主键(Rowids)作为值/参数(foreignkeys)传递到第三个表中,该表返回空值。

Here is my SP:- 这是我的SP:

    (1st Table)    
   INSERT INTO [kk_admin].[FA_Master](FA_Name,FA_CSession,FA_MSession) Values
             (@FA_Name,@Session_Id,@Session_Id)

   SELECT @**FA_ID=FA_ID** FROM [kk_admin].[FA_Master] where FA_Name=@FA_Name  

    (2nd Table)  
   INSERT INTO [kk_admin].[Dept_Master](Dept_Name,Dept_CSession,Dept_MSession) Values  
   (@Dept_Name,@Session_Id,@Session_Id)  

   SELECT @**Dept_id=Dept_id** from [kk_admin].[Dept_Master] where Dept_Name=@Dept_Name   

   (3rd Table)
   INSERT INTO [kk_admin].[Category_Master] (**FA_Id**,**Dept_Id**,Category_Name,Category_CSession,Category_MSession) Values    (@**FA_ID**,@**Dept_Id**,@Category_Name,@Session_Id,@Session_Id)  

Hope everyone understood what I have explained. 希望每个人都明白我的解释。

Plz Help me, 请帮我,
Iam running out of time. Iam时间用完了。 Plz help me. 请帮我。 Thank You in Advance, 先感谢您,
Brightsy Brightsy

You can use an OUTPUT clause (assuming you're using SQL Server 2005) to capture the primary key for the two rows you're inserting with the first two queries. 您可以使用OUTPUT子句(假设您使用的是SQL Server 2005)来捕获要通过前两个查询插入的两行的主键。 You can capture the values into a temporary table. 您可以将值捕获到临时表中。 [I previously wrote that you could use a regular variable, but that's not supported.] Example code: [我以前写过,您可以使用常规变量,但是不支持。]示例代码:

CREATE TABLE #FA_Master_ID ( ID int );
CREATE TABLE #Dept_Master_ID ( ID int );

INSERT kk_admin.FA_Master ( FA_Name, FA_CSession, FA_MSession )
OUTPUT INSERTED.ID INTO #FA_Master_ID
VALUES ( @FA_Name, @Session_Id, @Session_Id );

INSERT kk_admin.Dept_Master ( Dept_Name, Dept_CSession, Dept_MSession )
OUTPUT INSERTED.ID INTO #Dept_Master_ID
VALUES ( @Dept_Name, @Session_Id, @Session_Id );

INSERT kk_admin.Category_Master ( **FA_Id**, **Dept_Id**, Category_Name, Category_CSession, Category_MSession )
SELECT  **FA_Id** = ( SELECT TOP 1 ID FROM #FA_Master_ID ),
        **Dept_Id** = ( SELECT TOP 1 ID FROM #Dept_Master_ID ),
        Category_Name = @Category_Name,
        Category_CSession = @Session_Id,
        Category_MSession = @Session_Id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM