如何在SQL中将在主sp中创建的临时表的名称传递给内部sp?

时间:2018-10-05 08:20:32

标签: sql sql-server

这些是示例表

create procedure Sp_Details
@brandid int,
@productid int,
@IsShow bit
as
begin
    select SomeColumns
    into #tableForMechanism   
    from SomeData

    if @IsShow = 0 then
    begin
       select SomeColumns from #tableForMechanism  
    end 
    else
    begin
         -- Here want to pass table name created by this sp now
         -- So can check that table is exist.
         -- Because Inner sp is generic.From next 20 sp,have to call 
         -- same sp.That's why can't give same temp table name because
         -- their structure may be different

          exec innerDetailsProcedure 
    end          

end
go

create procedure innerDetailsProcedure
@tablename varchar(2000)
as
begin
    if OBJECT_ID(@tablename) is NULL    
    begin 
        raiserror('Table for mechanism is not created', 16, 1)
        return -1
    end
else
begin
      --update that table
       update @tablename
       set price = REPLACE(Price, (SUBSTRING(Price,2, 3*@Isshow)), 'xxx')
end


end

如果Isshow为假,则显示整个价格,否则隐藏第二至第四位数字(价格为5位数字)

fileHandle = h5py.File('calibration.hdf', 'r')
self.xcalib = fileHandle['x']
fileHandle.close()

创建的临时表名称的大小是多少?如何立即获取此sp为该会话创建的临时表?

1 个答案:

答案 0 :(得分:0)

所以您想在proc内部检查#temp表是否存在,基于该proc参数中的表名?如果是这样,请参见下文。您必须在检查中以OBJECT_ID()作为前缀数据库名称tempdb:

CREATE PROC p  @t1 sysname ,@t2 sysname AS IF OBJECT_ID('tempdb..' + @t1) IS NOT NULL  PRINT '@t1 exists'

IF OBJECT_ID('tempdb..' + @t2) IS NOT NULL  PRINT '@t2 exists' GO

CREATE TABLE #t(c1 int)

EXEC p @t1 = '#t', @t2 = '#doesntexist'