执行 sp_executesql 创建一个临时表后,外部是不是不能访问?
declare @strSql2 nvarchar(max)
set @strSql2=('select distinct [生产订单号] INTO #table_fanxiu_temp1 FROM  table1')
exec sp_executesql @strSql2select * from #table_fanxiu_temp1
消息 208,级别 16,状态 0,第 7 行
对象名 '#table_fanxiu_temp1' 无效。最后提示我的错误。哎。这个临时表怎么只有在一个区间才起作用呢?
求救,我现在应该怎么办?谢谢。

解决方案 »

  1.   

    declare @strSql2 nvarchar(max)
    set @strSql2=('select distinct [生产订单号] INTO ##table_fanxiu_temp1 FROM  table1')
    exec sp_executesql @strSql2select * from ##table_fanxiu_temp1
    作用域的问题
      

  2.   

    对啊。
    那有没有什么办法来解决
    我主要的是因为还要用上
    @whereStr_1 这个做为外部条件。
    那我只能把语句写在引号''中啊。
    有没什么办法解决呢?我后面的语法不想把SQL语句全部写在引号'' 中。有些处理太麻烦。引号中很难操作。
    declare @strSql2 nvarchar(max)
    declare whereStr_1 nvarchar(max)
    set @whereStr_1=' WHERE [生产订单号]=''BM-5061006090039'''set @strSql2=('select distinct [生产订单号] INTO #table_fanxiu_temp1 FROM  #table_fanxiu'+@whereStr_1)
    exec sp_executesql @strSql2select * from #table_fanxiu_temp1