简体   繁体   中英

use sql stored procedure to insert data which is returned from a query stored in a table

sql server 2005 I have a stored procedure which I used to insert data into a table. Some of the data will need to come from the results of executing a query that is stored in a separate table.

The main problem that I keep hitting is not being able to properly execute the returned query. I have tried creating several functions over the past couple of days based on other posts that I have read, but I keep hitting sql errors with exec, execute, sp_executesql, etc.

I am going to paste several scripts that you can use to replicate my environment. I am hoping that someone can please provide an actual code sample which will execute the returned query for use within the stored proc insert function.

Thank you!!!

    CREATE TABLE [dbo].[CLIENT](
    [cli_id] [int] IDENTITY(1,1) NOT NULL,
    [cli_first_name] [varchar](100) NULL,

 CONSTRAINT [PK__CLIENT__07F6335A] PRIMARY KEY CLUSTERED 
(
    [cli_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


INSERT INTO CLIENT (cli_first_name, cli_last_name) values ('tom', 'smith');
go



CREATE TABLE [dbo].[ASSESSMENT_DATALABEL_LIST](
    [adl_ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [BoundName] [nvarchar](50) NOT NULL,
    [Query] [ntext] NOT NULL,
    [KeyFieldName] [nvarchar](50) NOT NULL,
    [Status] [nvarchar](20) NOT NULL,
 CONSTRAINT [PK_ASSESSMENT_DATALABEL_LIST] PRIMARY KEY CLUSTERED 
(
    [adl_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

INSERT INTO ASSESSMENT_DATALABEL_LIST (Name, BoundName, Query, KeyFieldName, Status) 
values ('Name, First', 'cli_first_name', 'select IsNull(cli_first_name,'''') as cli_first_name FROM CLIENT WHERE cli_id = @KeyFieldValue', 'cli_ID', 'Active')
go

INSERT INTO ASSESSMENT_DATALABEL_LIST (Name, BoundName, Query, KeyFieldName, Status) 
values ('Name, Last', 'cli_last_name', 'select IsNull(cli_last_name,'''') as cli_last_name FROM CLIENT WHERE cli_id = @KeyFieldValue', 'cli_ID', 'Active')
go


CREATE TABLE [dbo].[Item_Source]
(   
    [Item_ID] [int] IDENTITY(1,1) NOT NULL,
    [ItemType] [nvarchar](50) NOT NULL,
    [ItemCaption] [nvarchar] (50) NULL,
      [adl_ID] [int] NOT NULL
)
go

INSERT INTO Item_Source
(ItemType, ItemCaption, adl_ID) values 
('DATALABEL', 'First Name',1)
go

INSERT INTO Item_Source
(ItemType, ItemCaption, adl_ID) values 
('DATALABEL', 'Last Name',2)
go

CREATE TABLE [dbo].[Item_Destination]
(
    [ItemType] [nvarchar](50) NOT NULL,
    [ItemCaption] [nvarchar] (50) NULL,
      [ItemValue] [nvarchar](50) NULL
)
go


CREATE PROCEDURE [dbo].[spInsertStuff]

@cli_id int

AS

INSERT INTO Item_Destination
(ItemType, ItemCaption, ItemValue)
SELECT
ItemType, ItemCaption, [[[ VALUE OF EXECUTED QUERY FROM ADL TABLE --- dbo.FunctionToGetResultsOfStoredQuery(Item_Source.adl_id, @cli_id) ]]]
FROM Item_Source WHERE Item_Source.Item_ID IN (1,2)

-- this insert will insert both Item_Source rows into Item_Dest with one call.  The first row should have an ItemValue of Tom, the second row should have an ItemValue of Smith
GO

You may check this fiddle

The code of the Stored Procedure is:

CREATE PROCEDURE [dbo].[spInsertStuff]

@cli_id int

AS


DECLARE @SQL AS VARCHAR(MAX)
DECLARE @ADL_ID AS INT

DECLARE MyCURSOR
CURSOR FOR 
SELECT QUERY, ADL_ID FROM ASSESSMENT_DATALABEL_LIST

OPEN MyCURSOR   
FETCH NEXT FROM MyCURSOR INTO @SQL, @ADL_ID   

WHILE @@FETCH_STATUS = 0   
BEGIN   

        SET @SQL = REPLACE(@SQL,'@KeyFieldValue',@cli_id)
        DECLARE @Temp AS TABLE ([Value] [nvarchar](50))

        INSERT INTO @Temp
        EXEC (@SQL)

        INSERT INTO Item_Destination
        (ItemType, ItemCaption, ItemValue)
        SELECT
        ItemType, ItemCaption, (SELECT [Value] FROM @Temp)
        FROM Item_Source 
        WHERE Item_Source.adl_ID = @ADL_ID   

        DELETE FROM @Temp

        FETCH NEXT FROM MyCURSOR INTO @SQL, @ADL_ID   
END   

CLOSE MyCURSOR   
DEALLOCATE MyCURSOR

GO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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