如何在一个存储过程中创建全局临时表,并在另一个存储过程中使用它

huangapple go评论91阅读模式
英文:

How to creating a global temp table in one stored procedure, and use it in another stored procedure

问题

I have a dynamic stored procedure that returns a result set.

  1. 创建或更改存储过程[test].[proc]
  2. (@id INT,
  3. @temp_table_name VARCHAR(50) = '')
  4. AS
  5. BEGIN
  6. SET @sql = N'从测试表 con 中选择 *';
  7. IF (LEFT(@temp_table_name, 2) = '##')
  8. BEGIN
  9. DECLARE @move_to_temp_sql varchar(max);
  10. SET @move_to_temp_sql = REPLACE(@sql, '从测试表 con', 'INTO ' + @temp_table_name + ' FROM 测试表 con');
  11. END
  12. 执行(@sql)
  13. END

当我从另一个存储过程调用存储过程时,我传递了临时表的名称。

  1. [test].[proc] 1, '##test'

我想这样访问全局临时表##test,如下所示:

  1. SELECT * FROM ##test;

当我尝试这样做时,我收到错误消息

无效的对象名称'##test'

请注意:表的结构会有所变化,因此我不能在我的调用存储过程中定义临时表,必须在动态存储过程中进行定义。

英文:

I have a dynamic stored procedure that returns a result set.

  1. CREATE OR ALTER PROCEDURE [test].[proc]
  2. (@id INT,
  3. @temp_table_name VARCHAR(50) = '')
  4. AS
  5. BEGIN
  6. SET @sql = N'SELECT * FROM test.table con';
  7. IF (LEFT(@temp_table_name, 2) = '##')
  8. BEGIN
  9. DECLARE @move_to_temp_sql varchar(max);
  10. SET @move_to_temp_sql = REPLACE(@sql, 'FROM test.table con', 'INTO ' + @temp_table_name + ' FROM test.table con');
  11. END
  12. EXECUTE(@sql)
  13. END

I'm passing the name of the temporary table when I'm calling the stored procedure from another stored procedure.

  1. [test].[proc] 1, '##test'

I would like to access the global temp table ##test from the calling stored procedure like this:

  1. SELECT * FROM ##test;

I get

> Invalid object name '##test'

when I try this.

Please note: the structure of the table would vary so I cannot define the temp table in my calling stored procedure, it has to be done in the dynamic stored procedure.

答案1

得分: 0

Your stored procedure never created the temp table you are intended to create, rather it's just selecting the records from test.table. That's why you are failing to find the temp table.

instead of EXECUTE(@sql) you need to use EXECUTE(@move_to_temp_sql).

Also you need to declare variable @sql in your stored procedure. Please try this. You will get your desired temp table.

You can also simply

  1. CREATE OR ALTER PROCEDURE [proc]
  2. (@id INT,
  3. @temp_table_name VARCHAR(50) = '')
  4. AS
  5. BEGIN
  6. DECLARE @sql varchar(max);
  7. IF (LEFT(@temp_table_name, 2) = '##')
  8. BEGIN
  9. SET @sql = 'select * INTO ' + QUOTENAME(@temp_table_name) + ' FROM test.table con';
  10. END
  11. EXECUTE(@sql)
  12. END
英文:

Your stored procedure never created the temp table you are intended to create, rather it's just selecting the records from test.table. That's why you are failing to find the temp table.

instead of EXECUTE(@sql) you need to use EXECUTE(@move_to_temp_sql).

Also you need to declare variable @sql in your stored procedure. Please try this. You will get your desired temp table.

You can also simply

  1. CREATE OR ALTER PROCEDURE [proc]
  2. (@id INT,
  3. @temp_table_name VARCHAR(50) = '')
  4. AS
  5. BEGIN
  6. DECLARE @sql varchar(max);
  7. IF (LEFT(@temp_table_name, 2) = '##')
  8. BEGIN
  9. SET @sql = 'select * INTO ' + QUOTENAME(@temp_table_name) + ' FROM test.table con';
  10. END
  11. EXECUTE(@sql)
  12. END

huangapple
  • 本文由 发表于 2023年4月20日 03:16:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定