Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 执行从数据库表返回的存储过程_Sql_Sql Server_Sql Server 2008_Stored Procedures - Fatal编程技术网

Sql 执行从数据库表返回的存储过程

Sql 执行从数据库表返回的存储过程,sql,sql-server,sql-server-2008,stored-procedures,Sql,Sql Server,Sql Server 2008,Stored Procedures,我正在使用sql server 2008 我有一个数据库表,其中有一列包含存储过程名称 我想查询返回存储过程名称列表的数据库表,并执行它们 存储过程类似,都有一个select语句。我要将此select语句中返回的数据插入到数据库表中 伪代码如下所示: INSERT INTO MyTable EXECUTE sp_executesql SELECT StoredProcedureName FROM Table execute sp_executesql 'execute ' + @storedp

我正在使用sql server 2008

我有一个数据库表,其中有一列包含存储过程名称

我想查询返回存储过程名称列表的数据库表,并执行它们

存储过程类似,都有一个select语句。我要将此select语句中返回的数据插入到数据库表中

伪代码如下所示:

INSERT INTO MyTable
EXECUTE sp_executesql SELECT StoredProcedureName FROM Table
execute sp_executesql 'execute ' + @storedprocedurename 

是否有人能够帮助我使用正确的sql来实现上述目标?

sp\u executesql接受unicode字符串而不是tsql语句。因此,您需要像这样执行您的过程:

INSERT INTO MyTable
EXECUTE sp_executesql SELECT StoredProcedureName FROM Table
execute sp_executesql 'execute ' + @storedprocedurename 
它将执行单个过程


您需要编写一些迭代过程来从源表中填充@storedprocedurename变量。

这与使用游标的@Coltech answer几乎相同

    DECLARE @spname VARCHAR(200)
    DECLARE @sql VARCHAR(1000)

    DECLARE your_cursor CURSOR FOR 
    SELECT spname
    FROM yourTable;

    OPEN your_cursor;

    FETCH NEXT FROM your_cursor 
    INTO @spname;

    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @sql = 'EXEC ' + @spname

        execute sp_executesql @sql

        FETCH NEXT FROM your_cursor 
        INTO @spname;
    END
    CLOSE your_cursor;