set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go-- =============================================
-- Author:lxsfg
-- Create date: 06/08/03
-- Description: nothing
-- =============================================
ALTER PROCEDURE [dbo].[sp_yp_GetInfoForPayTag]
@Tag varchar(100),
@Case varchar(200)
AS
BEGIN
SET NOCOUNT ON;
declare @dt table(id bigint identity(1,1),Infoid bigint,title varchar(200))
insert into @dt(Infoid,title) select id,title from dbo.WWW58COM_corp where charindex(@Tag,PayTag)>0 order by id desc
insert into @dt(Infoid,title) select id,title from dbo.WWW58COM_corp where charindex(@Tag,tag)>0 order by id desc
declare @inSql varchar(200)
set @inSql='select * from '+@dt+' where '+@Case+'  order by id'
exec(@inSql)
END
SET NOCOUNT OFF这个存储过程执行时报:
Msg 137, Level 15, State 2, Procedure sp_yp_GetInfoForPayTag, Line 17
必须声明标量变量 "@dt"。
怎么回事?在线等 谢谢~~

解决方案 »

  1.   

    --这样试一下
    ALTER PROCEDURE [dbo].[sp_yp_GetInfoForPayTag]
    @Tag varchar(100),
    @Case varchar(200)
    AS
    BEGIN
    SET NOCOUNT ON;
    declare @dt table(id bigint identity(1,1),Infoid bigint,title varchar(200))
    insert into @dt(Infoid,title) select id,title from dbo.WWW58COM_corp where charindex(@Tag,PayTag)>0 order by id desc
    insert into @dt(Infoid,title) select id,title from dbo.WWW58COM_corp where charindex(@Tag,tag)>0 order by id desc
    declare @inSql varchar(200)
    set @inSql='select * from @dt where '+@Case+'  order by id'
    exec(@inSql)
    END
    SET NOCOUNT OFF
      

  2.   

    可能是这样的,EXEC在执行时会另外开通一个连接,而你用的表变量只在一个连接有效,所以会出错的,用临时表吧
      

  3.   

    如果把
    set @inSql='select * from @dt where '+@Case+'  order by id'
    exec(@inSql)
    改成
    select * from @dt where @Case  order by id报的错误就变了变成 where后边非布尔值
      

  4.   

    ALTER PROCEDURE [dbo].[sp_yp_GetInfoForPayTag]
    @Tag varchar(100),
    @Case varchar(200)
    AS
    BEGIN
    SET NOCOUNT ON
    ----如果临时表已经存在则删除该临时表
    if object_id('tempdb..#tmp') is not null
          drop table #tmp
    ----创建临时表
    create table #tmp(id bigint identity(1,1),Infoid bigint,title varchar(200))
    ----向临时表中插入数据
    INSERT into #tmp(Infoid,title) 
    select id,title from dbo.WWW58COM_corp 
    where charindex(@Tag,PayTag)>0 order by id desc
    INSERT into #tmp(Infoid,title) 
    select id,title from dbo.WWW58COM_corp 
    where charindex(@Tag,tag)>0 order by id desc
    ----执行查询
    declare @inSql varchar(200)
    set @inSql='select * from #tmp where '+ @Case + '  order by id'
    exec(@inSql)
    ----删除临时表
    drop table #tmp
    END
    SET NOCOUNT OFF