使用sql存储过程插入从存储在表中的查询返回的数据

时间:2013-08-09 19:46:18

标签: sql sql-server sql-function

sql server 2005 我有一个存储过程,我用它将数据插入表中。某些数据需要来自执行存储在单独表中的查询的结果。

我一直遇到的主要问题是无法正确执行返回的查询。我已经尝试根据我读过的其他帖子在过去几天创建了几个函数,但是我继续使用exec,execute,sp_executesql等来解决sql错误。

我将粘贴几个可用于复制环境的脚本。我希望有人能够提供一个实际的代码示例,它将执行返回的查询以便在存储的proc插入函数中使用。

谢谢!!!

    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

1 个答案:

答案 0 :(得分:2)

您可以查看this fiddle

存储过程的代码是:

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