示例代码 create table #tmpWhQoh ( Wh bigint, itemcode NvarChar(100), itemid bigint, warehouseid bigint, storeuomqty decimal(19,2) ) declare @sql varchar(1000) = ' select A.Wh , A.ItemInfo_ItemCode as itemcode,A.ItemInfo_ItemID as itemid,A.Wh as warehouseid, (case when (isnull(A.StoreQty,0)-isnull(B.StoreUOMQty,0))<0 then 0 else (isnull(A.StoreQty,0)-isnull(B.StoreUOMQty,0)) end) as storeuomqty from InvTrans_WhQoh A left join #tranQty B on A.ItemInfo_ItemID=B.ItemInfo_ItemID ' if(dbo.fn_isEmpty(@TransferOutWh)=0) set @sql += ' and A.Wh = ''' + @TransferOutWh + '''' insert into #tmpWhQoh(Wh,itemcode,itemid,warehouseid,storeuomqty) exec(@sql) --执行SQL插入到临时表