Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用插入值从查找Sql返回字符串_C#_Sql_Stored Procedures - Fatal编程技术网

C# 使用插入值从查找Sql返回字符串

C# 使用插入值从查找Sql返回字符串,c#,sql,stored-procedures,C#,Sql,Stored Procedures,我有一个将客户插入SQL Server数据库的存储过程: @Name varchar(50), @Country int, @DateRegistered datetime, @NumberOfEmployees int, @ID int output, AS INSERT INTO AccountManagementChristian.dbo.Customer(Name, CountryID, DateRegistered, EmployeeRangeID) VALUES(@Name,

我有一个将客户插入SQL Server数据库的存储过程:

@Name varchar(50),
@Country int,
@DateRegistered datetime, 
@NumberOfEmployees int,
@ID int output,

AS

INSERT INTO AccountManagementChristian.dbo.Customer(Name, CountryID, DateRegistered, EmployeeRangeID)
VALUES(@Name, @Country, @DateRegistered, @NumberOfEmployees)
SET @ID = @@IDENTITY
我还有一个带有国家ID和国家名称的查找表,还有一个带有EmployeeRange ID和员工人数的查找表。国家名称和员工人数都是字符串


插入时,我需要能够返回与插入的国家ID相对应的国家名称字符串,以及与国家名称和国家ID相同的国家名称字符串?

我很难真正理解插入国家返回选项的含义,因为这里与此无关。事实上,如果您正在插入国家,然后使用它,那么只需复制DECLARE@CustomerTab和输出插入行即可捕获该数据。更新后的脚本插入数据并返回我认为您需要的字段。希望能有帮助。下:

@Name varchar(50),
@Country int,
@DateRegistered datetime, 
@NumberOfEmployees int,
@ID int output

AS   

DECLARE @CustomerTab            AS Table (CustomerID      INT)

INSERT INTO AccountManagementChristian.dbo.Customer(Name, CountryID, DateRegistered, EmployeeRangeID)
    OUTPUT
        INSERTED.ID INTO @CustomerTab(CustomerId)
VALUES(@Name, @Country, @DateRegistered, @NumberOfEmployees)
--SET @ID = @@IDENTITY

select 
    Customer.Id
    ,Country.ID
    ,Country.[Country Name]
    ,EmployeeRange.[Number of Employees]
from 
    Customer
inner join 
    @CustomerTab CustomerTab 
on
    Customer.Id = CustomerTab.CustomerID
inner join
    Country
on
    Customer.CountryId = Country.Id
inner join
    EmployeeRange
on
    Customer.EmployeeRangeID = EmployeeRange.ID

如果我理解正确,您需要在存储过程输入变量中插入客户信息,然后返回新插入客户的详细信息。在这种情况下,您的INSERT for customer是正确的,您需要像在普通SELECT中一样选择所需的字段,而不是为customer ID提供输出。下面应该做你想做的

@名字是varchar50, @国家国际, @日期注册日期时间, @雇员人数 像 DECLARE@ID INT-将其移动为标准变量 插入AccountManagementChristian.dbo.CustomerName,CountryID, DateRegistered,EmployeeRangeID VALUES@Name,@Country,@DateRegistered,@NumberOfEmployees 设置@ID=@@IDENTITY -添加此选择以返回所需信息, -使用@ID值获取新客户的详细信息 选择C.ID作为CustomerID,C.Name, CL.ID作为CountryID,CL.CountryName, ER.ID为EmployeeRangeID,ER.NumberOfEmployees-,其他必填字段 从AccountManagementChristian.dbo.Customer作为C内部联接 CountryLookup作为C.Country上的CL=CL.ID内部联接 EmployeeRange作为C.EmployeeRangeID=ER.ID上的ER 其中C.ID=@ID
嗨,谢谢你的快速回复!我在声明@CountryTab为表@Country intMsg 102,级别15,状态1,过程InsertCustomer,第11行“@Country”附近出现语法错误。需要为临时表别名。-已修复。注意:在这种情况下,您应该使用范围\标识,而不是@标识。