仅当表为空时才插入行 [英] Insert row only if table is empty

查看:92
本文介绍了仅当表为空时才插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试下面的代码,只有当表(temp2)为空时才将数据插入我的临时表。但它显示空白表即使表中确实有数据。我有什么问题吗?
b

提前付款



INSERT INTO#temp1(Type)

SELECT'0'

不存在(选择*

来自#temp2)

Hi,

I am trying below code to insert data into my temp table only if the table(temp2) is empty .But It shows blank table even though the table does have data in it. is there something i am doing wrong?

Thanks in advance

INSERT INTO #temp1 (Type)
SELECT '0'
WHERE NOT EXISTS (SELECT *
  from #temp2)

推荐答案

希望以下查询可以帮助您

Hope below query may help you 

CREATE TABLE #temp1 (Name VARCHAR(20))
GO

DECLARE @Count INT 
  SET @Count = (SELECT COUNT(*) FROM #temp1)
  --PRINT @Count
  IF @Count = 0
  BEGIN 
  INSERT INTO #temp1
  SELECT 'chetan'
  END
  ELSE 
  Print 'Table has value and cant insert'

如果我的帖子可以帮助你,请将我标记为答案。

Please mark me as answer if my post helps you.

Br

ChetanV


这篇关于仅当表为空时才插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆