SQL在If和Else块中插入临时表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL在If和Else块中插入临时表相关的知识,希望对你有一定的参考价值。
我正在尝试根据SQL 2005中的条件结果填充临时表。临时表将具有相同的结构,但将根据条件使用不同的查询填充。以下简化的示例脚本在ELSE
块INSERT INTO
的语法检查中失败,错误为:
数据库中已经有一个名为“#MyTestTable”的对象。
DECLARE @Id int
SET @Id = 1
IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE #MyTestTable
IF (@Id = 2) BEGIN
SELECT 'ABC' AS Letters
INTO #MyTestTable;
END ELSE BEGIN
SELECT 'XYZ' AS Letters
INTO #MyTestTable;
END
我可以在IF/ELSE
语句之前创建临时表,然后在条件块中执行INSERT SELECT
语句,但该表将有很多列,我试图提高效率。这是唯一的选择吗?或者有一些方法可以使这项工作?
谢谢,马特
您遇到的问题不是您正在填充临时表,而是您正在尝试创建表。 SQL会解析您的脚本并发现您尝试在两个不同的位置创建它,因此会引发错误。要意识到“执行路径”不可能同时触及两个创建状态,这并不够聪明。使用动态SQL将无法正常工作;我试过了
DECLARE @Command varchar(500)
DECLARE @Id int
SET @Id = 2
IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE #MyTestTable
IF (@Id = 2) BEGIN
SET @Command = 'SELECT ''ABC'' AS Letters INTO #MyTestTable'
END ELSE BEGIN
SET @Command = 'SELECT ''XYZ'' AS Letters INTO #MyTestTable'
END
EXECUTE (@Command)
select * from #MyTestTable
但临时表只持续动态会话。所以,唉,看起来你必须首先声明表然后填充它。也许是编写和支持的笨拙代码,但它会足够有效地执行。
回答迟了8年,但我很惊讶没人想到:
select * into #MyTempTable from...
where 1=2
IF -- CONDITION HERE
insert into #MyTempTable select...
ELSE
insert into #MyTempTable select...
简单,快速,而且有效。不需要动态sql
在您提供的方案中,您可以执行此操作
DECLARE @Id int
SET @Id = 1
IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE #MyTestTable
SELECT
CASE WHEN (@Id = 2)
THEN 'ABC'
ELSE 'XYZ'
END AS Letters
INTO #MyTestTable;
但是否则你需要在if statement
之前创建这样的表
Create Table #MyTestTable (
MyValue varchar(3)
)
IF (@Id = 2) BEGIN
Insert Into (MyValue)
SELECT 'ABC' AS Letters;
END ELSE BEGIN
Insert Into (MyValue)
SELECT 'XYZ' AS Letters;
END
如果临时表不能预先创建并且不希望将核心逻辑放在动态SQL中,那么我使用这个解决方案。
IF 1 = 1 -- Replace with actual condition
BEGIN
SELECT * INTO #tmp1 FROM dbo.Table1
END
ELSE
BEGIN
SELECT * INTO #tmp2 FROM dbo.Table2
END
-- Inserting data into global temp table so sql server can't complain on not recognizing in a context
DECLARE @Command VARCHAR(MAX)
IF OBJECT_ID('tempdb..#tmp1') IS NOT NULL
BEGIN
SET @Command = 'SELECT * INTO ##tmp FROM #tmp1'
END
ELSE
BEGIN
SET @Command = 'SELECT * INTO ##tmp FROM #tmp2'
END
EXECUTE(@Command)
SELECT * INTO #tmpFinal FROM ##tmp -- Again passing data back to local temp table from global temp table to avoid seeing red mark
IF OBJECT_ID('tempdb..##tmp') IS NOT NULL DROP TABLE ##tmp
IF OBJECT_ID('tempdb..#tmp1') IS NOT NULL DROP TABLE #tmp1
IF OBJECT_ID('tempdb..#tmp2') IS NOT NULL DROP TABLE #tmp2
SELECT * FROM #tmpFinal
IF OBJECT_ID('tempdb..#tmpFinal') IS NOT NULL DROP TABLE #tmpFinal
这是一个老问题,但对于其他人来到这里:
用户Philip Kelley给出的动态SQL答案不适用于本地临时表(#Mytemp
)。您可以做的是创建动态SQL以将其插入到全局临时表(##MyTemp
)中,以后可以将其删除。
DECLARE @Command varchar(500)
DECLARE @Id int
SET @Id = 2
IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE ##MyTestTable
IF (@Id = 2) BEGIN
SET @Command = 'SELECT ''ABC'' AS Letters INTO ##MyTestTable'
END ELSE BEGIN
SET @Command = 'SELECT ''XYZ'' AS Letters INTO ##MyTestTable'
END
EXECUTE (@Command)
select * from ##MyTestTable
DROP ##MyTestTable
这段代码可以帮到你
--creating temptable using columns of two existing tables
--you can create your temp table Using other methods
select top 0 VI.*,VU.FullName
into #mytemptable
from dbo.Items VI inner join
dbo.Users as VU
on VU.Id=VI.Id
--insert your data base on your condition
if(i<2) --First Condition
begin
INSERT INTO #mytemptable
SELECT VI.*,VU.FullName
from dbo.Items VI inner join
dbo.Users as VU
on VU.Id=VI.Id
end
Else if(2<i) --Second Condition
begin
INSERT INTO #mytemptable
SELECT VI.*,VU.FullName
from dbo.Items VI inner join
dbo.Users as VU
on VU.Id=VI.Id
end
select * from #mytemptable --show result
drop table #mytemptable --drop table if its needed
此代码适用于sql server 2014我不知道它是否适用于sql 2005
我试过这个:
SELECT S1.* INTO #MytestTable
FROM
( SELECT 'ABC' AS Letters WHERE 1 = CASE @Id=2 THEN 1 ELSE 2 END
UNION
SELECT 'XYZ' AS Letters WHERE 1 = CASE @Id=1 THEN 1 ELSE 2 END
) AS S1
如果以后需要向#MyTestTable添加列,这个解决方案会更好,因为否则您必须在重新运行脚本之前将其删除,这在测试条件下很烦人。
在两种情况下,您可以在选择INTO之前删除该表。例如:
DECLARE @Id int
SET @Id = 1
IF (@Id = 2) BEGIN
IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE #MyTestTable
SELECT 'ABC' AS Letters
INTO #MyTestTable;
END ELSE BEGIN
IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE #MyTestTable
SELECT 'XYZ' AS Letters
INTO #MyTestTable;
END
评论后更新:
那很烦人。
两个单独的临时表怎么样?然后在If / Else登录后,检查每个是否存在,如果存在,选择第三个临时表?这可能不会很好,但是否重要取决于你需要什么。
以上是关于SQL在If和Else块中插入临时表的主要内容,如果未能解决你的问题,请参考以下文章
为啥使用三元运算符返回字符串与在等效 if/else 块中返回的代码有很大不同?
如何在 UNIX shell 脚本中执行临时存储过程?是不是可以在 sql-plus 中使用 IF-ELSE/WHILE 循环?