当没有行时如何插入行

发布于 2024-12-13 20:26:02 字数 408 浏览 3 评论 0原文

我有一个应用程序,需要部署给客户。通过以下方式检查他们是否已订阅该应用程序。

他们的数据库中会有一个表。如果此表中已有条目,那么我可以将我的模块添加到其中。记录看起来像这样。

MODULE_ID 描述 DISPLAY_TEXT ACTIVE 12 Census NULL 1

我所要做的就是将我的模块添加到代码中。我有 16 和 17 模块 ID,名称是“Orders”和“Missed Medicine”。

Module_ID 的长度为 18 数字

描述为 varchar(50)

Display_test varchar(25) 允许空值。

Active 是 1 个数字,允许空值。

我需要编写 INSERT 代码,但前提是表中已存在此类行。最好的方法是什么?

I have an app and need for deploying to customers. to check in the following way if they have subscribed to the app.

they will have a table on their database. If there are aleady entries in this table, then I can add my modules to it. The record looks like this.

MODULE_ID DESCRIPTION DISPLAY_TEXT ACTIVE
12 Census NULL 1

All that I have to do is to add my modules to this in the code. I have 16 and 17 module ID and the names are 'Orders' and 'Missed Medicine'.

Module_ID is len of 18 Numberic

Description is varchar(50)

Display_test varchar(25) allows nulls.

Active is 1 numeric allows nulls.

I need to code the INSERT but only if there are such rows already in the table. what is the best way for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2024-12-20 20:26:03

您可以使用以下内容:

DECLARE @HasData INT

SELECT @HasData = count(*)
FROM [table]

if (@HasData != 0)    
begin
    INSERT INTO [table] VALUES ('module1')
    INSERT INTO [table] VALUES ('module2')
    INSERT INTO [table] VALUES ('module3')
    -- etc
end

You could use the following:

DECLARE @HasData INT

SELECT @HasData = count(*)
FROM [table]

if (@HasData != 0)    
begin
    INSERT INTO [table] VALUES ('module1')
    INSERT INTO [table] VALUES ('module2')
    INSERT INTO [table] VALUES ('module3')
    -- etc
end
青衫负雪 2024-12-20 20:26:03

“SELECT COUNT(*) FROM YourTable;”

如果返回的值大于 0,则可以插入。

´SELECT COUNT(*) FROM YourTable;´

If that returns more then 0, you can insert.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文