我想根据一个名为'type‘的参数从不同的表中进行选择。我可以使用'CASE‘来选择from表吗?
我能用这样的东西吗?
select
a as abc
b as xyz
from ( CASE when @type = 1 then tblSales
when @type = 2 then tblTransfer
end
)发布于 2011-03-15 20:01:44
这就是你要找的吗?
select
a as abc,
b as xyz
from
tblSales
WHERE
@type = 1
UNION
select
a as abc,
b as xyz
FROM
tblTransfer
WHERE
@type = 2发布于 2011-03-15 19:54:57
我可能会这样做:
SELECT a AS abc, b AS xyz FROM tblSales WHERE @type=1
UNION ALL
SELECT a AS abc, b AS xyz FROM tblTransfer WHERE @type=2发布于 2011-03-15 19:56:53
Declare @Sql nvarchar(4000)
Declare @type int
SET @type = 1
SET @Sql = ''
set @Sql = @Sql + '
select * FROM '
IF @type = 1
BEGIN
set @Sql = @Sql + '[tblSales]'
END
ELSE
BEGIN
set @Sql = @Sql + 'tblTransfer'
END
print @sql
exec (@Sql)https://stackoverflow.com/questions/5311242
复制相似问题