1

I'm trying to combine an insert sql statement with a select statement, I successfully combined this statement:

insert into dbo.sessions (se_user) select user_id from dbo.users where username = 'bader'", badersql);

What I need now is to insert another value in the same sql statement, like this:

insert into dbo.sessions (se_user,*se_ip*) select user_id from dbo.users...

As you notice there is se_ip here in the second statement, the value will be from a text box, how can I combine it with my sql statement?

5 Answers 5

1
INSERT INTO dbo.sessions (se_user, se_ip)
SELECT user_id, @ipAddress FROM dbo.users WHERE username = @username
Sign up to request clarification or add additional context in comments.

Comments

1

You can select constant from a parameter:

using (var command = new SqlCommand(@"insert into dbo.sessions (se_user, se_ip) 
        select user_id, @text from dbo.users...", connection) {
    command.Parameters.AddWithValue("text", myTextBox.Text);

    command.ExecuteNonQuery();
}

1 Comment

could you please provide your answer with an example ? , thanks
1
insert into dbo.sessions 
    (se_user,se_ip) 
    select user_id, @TextBoxValue
        from dbo.users...

Comments

0

You can use the first insert statement that you have mentioned in your question and then add an update statement to modify the data as you need to

OR

I am assuming you have the value of the textbox in a variable and so you can use something like SELECT user_id,@yourtextboxvalue from userstable...

Comments

0

You'll need to add another field to the select portion of your insert statement

insert into dbo.sessions (se_user,*se_ip*) select user_id, other_value from dbo.users ...

If the value comes from a text box,, it depends on how you're running the insert statement. You'll probably have to set a parameter to your command object. see this link for more details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.