Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>INSERT</code> command comes in two flavors:</p> <p><strong>(1)</strong> either you have all your values available, as literals or SQL Server variables - in that case, you can use the <code>INSERT .. VALUES()</code> approach:</p> <pre><code>INSERT INTO dbo.YourTable(Col1, Col2, ...., ColN) VALUES(Value1, Value2, @Variable3, @Variable4, ...., ValueN) </code></pre> <p>Note: I would recommend to <strong>always</strong> explicitly specify the list of column to insert data into - that way, you won't have any nasty surprises if suddenly your table has an extra column, or if your tables has an <code>IDENTITY</code> or computed column. Yes - it's a tiny bit more work - <strong>once</strong> - but then you have your <code>INSERT</code> statement as solid as it can be and you won't have to constantly fiddle around with it if your table changes.</p> <p><strong>(2)</strong> if you <strong>don't</strong> have all your values as literals and/or variables, but instead you want to rely on another table, multiple tables, or views, to provide the values, then you can use the <code>INSERT ... SELECT ...</code> approach:</p> <pre><code>INSERT INTO dbo.YourTable(Col1, Col2, ...., ColN) SELECT SourceColumn1, SourceColumn2, @Variable3, @Variable4, ...., SourceColumnN FROM dbo.YourProvidingTableOrView </code></pre> <p>Here, you must define exactly as many items in the <code>SELECT</code> as your <code>INSERT</code> expects - and those can be columns from the table(s) (or view(s)), or those can be literals or variables. Again: explicitly provide the list of columns to insert into - see above.</p> <p>You can use <strong>one or the other</strong> - but you <strong>cannot</strong> mix the two - you cannot use <code>SELECT</code> and have a <code>VALUES(...)</code> clause in the middle of it - pick one of the two - stick with it.</p> <p>So in your concrete case, just use:</p> <pre><code>INSERT INTO dbo.table_name (ID, Name, Email) SELECT userID, userName, 'example@yahoo.com' FROM users_table WHERE condition1 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload