0

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
4

1 回答 1

2

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
于 2013-08-09T20:09:43.727 回答