Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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
Sql server 添加过程时,SQL子查询返回了多个值_Sql Server_Tsql_Stored Procedures - Fatal编程技术网

Sql server 添加过程时,SQL子查询返回了多个值

Sql server 添加过程时,SQL子查询返回了多个值,sql-server,tsql,stored-procedures,Sql Server,Tsql,Stored Procedures,我尝试在SQL Server中创建过程,以便将users\u id值传递给另一个表 但是,在填充数据时,asp.net返回一个错误,即子查询返回的值超过1。以下是我的程序: CREATE PROCEDURE [dbo].[insert_rent] @comment_info NVARCHAR(MAX), @booth_size NVARCHAR(MAX), @customer_type NVARCHAR(MAX), @booth_type NVARCHAR(MAX

我尝试在SQL Server中创建过程,以便将users\u id值传递给另一个表

但是,在填充数据时,asp.net返回一个错误,即子查询返回的值超过1。以下是我的程序:

CREATE PROCEDURE [dbo].[insert_rent]
    @comment_info NVARCHAR(MAX),
    @booth_size NVARCHAR(MAX),
    @customer_type NVARCHAR(MAX),
    @booth_type NVARCHAR(MAX),
    @customer_email TEXT,
    @customer_mobile TEXT
AS
    DECLARE @Users_Id INT 

    IF EXISTS (SELECT Id FROM Users)
    BEGIN
        SET @Users_Id = (SELECT Id FROM Users)

        INSERT INTO Rent (rent_comments, booth_size, customer_type, booth_type, customer_email, customer_mobile, Users_id) 
        VALUES (@comment_info, @booth_size, @customer_type, @booth_type, @customer_email, @customer_mobile, @Users_Id)
    END
这是我的密码:

String CS = ConfigurationManager.ConnectionStrings["BoothsConnectionString1"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
    SqlCommand cmd = new SqlCommand("insert_rent", con);
    cmd.CommandType = CommandType.StoredProcedure;

    con.Open();
    cmd.Parameters.Add("@comment_info", SqlDbType.NVarChar).Value = comment_input.Text;
    cmd.Parameters.Add("@booth_size", SqlDbType.NVarChar).Value = size_dropwdown.SelectedValue.ToString();
    cmd.Parameters.Add("@customer_type", SqlDbType.NVarChar).Value = customer_type_dropdown.SelectedValue.ToString();
    cmd.Parameters.Add("@booth_type", SqlDbType.NVarChar).Value = booth_type_dropdown.SelectedValue.ToString();
    cmd.Parameters.Add("@customer_email", SqlDbType.NVarChar).Value = customer_email.Value;
    cmd.Parameters.Add("@customer_mobile", SqlDbType.NText).Value = customer_mobilenumber.Value;

    cmd.ExecuteNonQuery();

我相当肯定我的过程有问题。

您正在检查用户中是否存在任何Id

只要您的表中至少有一行,这将始终是正确的

在下一步中,尝试将变量设置为值:

set @Users_Id = (select Id from Users)
但如果没有配件,您将获得表用户包含的所有ID


使用子选择为标量结果设置变量。

您使用的是哪种dbms?这段代码是特定于产品的。visual studioset@Users\u Id=SelectID from Users中内置的一段代码-这里有一个问题,每行插入的代码都有很多Id。为什么不将UserID作为另一个参数传入呢?除非应用程序中的每个用户都有一个不同的数据库用户,否则这正是您需要做的。
set @Users_Id = (select Id from Users)