简体   繁体   English

从存储过程执行 T-SQL

[英]T-SQL Execute from a stored procedure

I am trying to build an SQL inside a stored procedure and execute it using我正在尝试在存储过程中构建一个 SQL 并使用它执行它

EXEC  sp_executesql

Now I defined a local table and tried to pass it in need to pass it in现在我定义了一个本地表并尝试将其传递给需要传递它

CREATE TYPE mytabletypeAS TABLE (
    StartDate DATETIME,
    EndDate DATETIME,
    Amount MONEY,
    AccountId INT
);

The following happens in my stored procedure, what I am trying to do is to return the output produced by EXEC sp_executesql :在我的存储过程中发生以下情况,我要做的是返回EXEC sp_executesql生成的 output :

CREATE PROCEDURE attributevalues.sp_EvalClearingNetSpend  
AS 
BEGIN
    DECLARE @OutPutTable AS mytabletype;
    DECLARE @Sql AS NVARCHAR(MAX);
    SET @Sql  = 'INSERT INTO @OutPutTable SELECT StartDate,EndDate,Amount, AccountId FROM table1';

    EXEC  sp_executesql @Sql, N'@OutPutTable mytabletype OUTPUT', @OutPutTable OUTPUT;

    SELECT * FROM @OutPutTable 
END

The above is saying I cannot pass in OUTPUT with @OutPutTable上面说我不能用@OutPutTable OUTPUT

Help!!!帮助!!!

all i need to know is, is there a way I can get the values from a statement executed via EXEC and return it from my SP我需要知道的是,有没有一种方法可以从通过 EXEC 执行的语句中获取值并从我的 SP 返回

Yes.是的。 But you can't do it with a table variable.但是你不能用表变量来做到这一点。 You can pass a table variable into the nested batch with sp_executesql just like you pass one to a stored procedure, but it has to be marked readonly, and so you can't modify it.您可以使用 sp_executesql 将表变量传递到嵌套批处理中,就像将表变量传递给存储过程一样,但是它必须标记为只读,因此您不能修改它。

You can see and modify existing temporary tables in nested batches, eg您可以在嵌套批次中查看和修改现有的临时表,例如

drop table if exists table1 
go
create table table1
(
    StartDate DATETIME,
    EndDate DATETIME,
    Amount MONEY,
    AccountId INT
)

insert into table1 values (getdate(),getdate(),1,1)

go
CREATE OR ALTER PROCEDURE EvalClearingNetSpend  
AS 
BEGIN
    create table #t( 
      StartDate DATETIME,
      EndDate DATETIME,
      Amount MONEY,
      AccountId INT)
    Declare @Sql as NVARCHAR(MAX);
    SET @Sql  = 'INSERT INTO #T SELECT StartDate,EndDate,Amount, AccountId FROM table1';
    EXEC  sp_executesql @Sql
    SELECT * FROM #T 
END
go

exec EvalClearingNetSpend

outputs输出

StartDate               EndDate                 Amount                AccountId
----------------------- ----------------------- --------------------- -----------
2021-03-30 08:48:32.350 2021-03-30 08:48:32.350 1.00                  1

This is an XY Problem .这是一个XY 问题 There is no need for the level of complexity you have put in the procedure.不需要您在程序中设置的复杂程度。 The table TYPE , the call to sys.sp_executesql , none of it is needed.TYPE ,对sys.sp_executesql的调用,都不需要。 Just put the SELECT statement of your "dynamic" query (it's not dynamic, as there's no object injection) in your Procedure:只需将“动态”查询的SELECT语句(它不是动态的,因为没有 object 注入)在您的程序中:

CREATE PROCEDURE attributevalues.EvalClearingNetSpend AS --Removed sp_ prefix
BEGIN
    SELECT StartDate,EndDate,Amount, AccountId FROM table1;
END;
GO

This completely avoids the error, that you are trying to use a table TYPE as an OUTPUT parameter, because you can't, but you don't need one here.这完全避免了错误,即您尝试使用表TYPE作为OUTPUT参数,因为您不能,但您在这里不需要。

Not sure why you would go through the trouble of creating a Table Type parameter and populating it inside your from and then selecting from it.不知道为什么您会通过创建表类型参数并将其填充到您的 from 中然后从中选择的麻烦来 go 。 But lets say you do have to do this for some reason.但是,假设您出于某种原因必须这样做。

You can achieve this by doing this, no need to use output parameters at all, output parameter is used to return a scalar value, here you are getting a table back and then selecting from it:您可以通过这样做来实现这一点,根本不需要使用 output 参数,output 参数用于返回一个标量值,这里你得到一个表,然后从中选择:

CREATE PROCEDURE usp_EvalClearingNetSpend  
AS 
BEGIN

Declare @Sql as NVARCHAR(MAX);
SET @Sql  = N'  DECLARE @OutPutTable AS mytabletype;

                INSERT INTO @OutPutTable 
                SELECT StartDate,EndDate,Amount, AccountId 
                FROM table1;

                SELECT * FROM @OutPutTable 
                ';
EXEC  sp_executesql @Sql

END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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