插入包含单个动态数据的多行

发布于 2025-01-16 01:35:31 字数 469 浏览 3 评论 0原文

我正在尝试将具有不同 @EmpId 的多行的相同数据插入表中。

追问:

CREATE PROCEDURE usp_SaveEmployee
    (@EmpIds varchar(50),  //1,2,3,4
     @StateId int,
     @CountryId int,
     @Comments varchar(50))
AS
BEGIN
    INSERT INTO dbo.tblEmpDetails (EmpId, StateId, CountryId, Comments)
        SELECT * 
        FROM [dbo].[FN_ListToTable] (',',@EmpIds)

    -- How can I add this: (@StateId, @CountryId, @Comments)
END

或者还有其他更好的方法来处理这个问题吗?

I'm trying to insert same data for multiple rows with different @EmpId into table.

Query:

CREATE PROCEDURE usp_SaveEmployee
    (@EmpIds varchar(50),  //1,2,3,4
     @StateId int,
     @CountryId int,
     @Comments varchar(50))
AS
BEGIN
    INSERT INTO dbo.tblEmpDetails (EmpId, StateId, CountryId, Comments)
        SELECT * 
        FROM [dbo].[FN_ListToTable] (',',@EmpIds)

    -- How can I add this: (@StateId, @CountryId, @Comments)
END

Or is there any other better way to handle this?

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

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

发布评论

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

评论(1

烟─花易冷 2025-01-23 01:35:31

只需在选择列表中定义正确的列即可:

INSERT INTO dbo.tblEmpDetails (EmpId, StateId, CountryId, Comments)
SELECT [<ReturnedColumn>], @StateId, @CountryId, @Comments
from dbo.FN_ListToTable (',', @EmpIds);

另请注意,SQL Server 有一个内置的string_split() 函数。

Simply define the correct columns in the select list:

INSERT INTO dbo.tblEmpDetails (EmpId, StateId, CountryId, Comments)
SELECT [<ReturnedColumn>], @StateId, @CountryId, @Comments
from dbo.FN_ListToTable (',', @EmpIds);

Also note that SQL Server has a built-in string_split() function.

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