存储过程中有临时表生成DataSet会失败

存储过程中如果有临时表,或者有复杂的业务逻辑,此时,要生成DataSet会失败。

CREATE PROCEDURE [dbo].[usp]
AS
BEGIN
     SET NOCOUNT ON

    SELECT *
    INTO #temptable
    FROM customs

    SELECT *
    FROM #temptable
END

处理方法:

1 首先修改存储过程,select * into  _temp from ….., 运行存储过程,生成一个中间表(_temp) ,

ALTER PROCEDURE [dbo].[usp]
AS
BEGIN
      SET NOCOUNT ON

    SELECT *
    INTO #temptable
    FROM customs

    SELECT *
    INTO _temp
    FROM #temptable
END

2 修改存储过程为 Select * from _temp,其他代码都注释掉。

ALTER PROCEDURE [dbo].[usp]
AS
BEGIN
      SET NOCOUNT ON

--    SELECT *
--    INTO #temptable
--    FROM customs
--
--    SELECT *
--    INTO _temp
--    FROM #temptable

    SELECT *
    FROM _temp

END

3 生成DataSet

4 还原存储过程

ALTER PROCEDURE [dbo].[usp]
AS
BEGIN
     SET NOCOUNT ON

    SELECT *
    INTO #temptable
    FROM customs

    SELECT *
    FROM #temptable
END

原文地址:https://www.cnblogs.com/emanlee/p/1529316.html