SQL Server Performance

Help on inserting to bridge table

Discussion in 'ALL SQL SERVER QUESTIONS' started by Bplvid, Apr 4, 2012.

  1. Bplvid New Member

    I've a stored procedure to insert into multiple tables with foreign key constraint.I created a temp table and populated from there.but my issue here is I just need to insert the newely created ID and another PrimaryKey ID to bridge table.I've bold font on my SP where i need help.The last insert fails.
    I need to insert newely inserted Parenttableid and Viewid to bridgetable.

    Code:
    @Parenttableid uniqueidentifier,
                @UserId uniqueidentifier;
        declare @ParenttableData table (id uniqueidentifier);
    
        set @UserId =(select UserId from Users where UserName= @Username)
    
        BEGIN
    
                    insert into parenttable(
                        Parenttableid,
                        UserId
    
                    )
                    output inserted.Parenttableid into @parentTableData(id)
                    values (
                        NEWID(),
                        @UserId,
    
                    );
    
                    select @parenttableid = id from @ParenttableData;
    
                    insert into items(
                      parenttableid,
                        itemsID,
    
                    )
                    values (
                        @parenttableid,
                        NEWID()
    
                    );
    [B]insert into bridgetable (parenttableID,viewId) values[/B]
    [B]      (@parenttableid,@viewID)[/B]
  2. Shehap MVP, MCTS, MCITP SQL Server

    First , Welcome to Forums

    Might you pass the error appears for you on Insert failure...?

Share This Page