What's the easiest way to select a row, modify couple of columns and insert it to the same table?
I'm trying to insert a new row based on another.
INSERT INTO table2 (column1, column2, column3)
SELECT column1, 'no', column3 FROM table2 WHERE column2 = 'yes'
Hopefully this is a bit clearer as to how you do this. As you can see, I've grabbed two columns from table2 and for the other column I used a text value for instead of the value for column2.
Other patterns you can use:
Combine a column and some other text (Assumes the column is already a string data type.
INSERT INTO table2 (column1, column2)
SELECT column1 + 'no', column2 FROM table2 WHERE column2 = 'yes'
Combine a column and some text, One example where the column is a string and one where it is not.
INSERT INTO table2 (column1, column2)
SELECT column1 + 'no', 'A' + cast(column2 as Varchar (10)) FROM table2 WHERE column2 = 'yes'
INSERT INTO table(column1,column2) SELECT column1, const2 FROM table ...
The select list will likely mix copied columns (by name) and changed columns (by desired values).
Say column is int
INSERT INTO table(column1,column2) SELECT column1 + 1, column2 -1 FROM table
You can multiply columns and perform string functions.
Related
How to move recent data with column name or given value doesn't match definition table. For example, I have a SalesInvoice table and want to move it to the PurchaseInvoice table, from which the table actually has the same column name but a different structure (eg SalesInvoiceID in SalesInvoice is in column 2 and SalesInvoiceID PurchaseInvoice is in column 6)
I've tried it up to here but there is still an error
BEGIN TRAN
INSERT INTO PurchaseInvoice
SELECT \* FROM SalesInvoice
WHERE SalesInvoice.SalesInvoiceID = '016'
You can do it by mentioning your columns in order :
INSERT INTO pbx_intovoice.tag (column1, column2, column3, column4)
select column1, column2, column3, column4
FROM SalesInvoice
WHERE SalesInvoice.SalesInvoiceID = '016'
If columns names are not the same as the receiver table you can still insert data from it :
INSERT INTO pbx_intovoice.tag (column1, column2, column3, column4)
select s.col1 as column1, s.col2 as column2, s.col3 as column3, s.col4 as column4
FROM SalesInvoice s
WHERE s.SalesInvoiceID = '016'
I want to search for a single value in 3 columns and return each occurrence of this value.
My query is based on the following answer regarding a similar request.
SELECT *
FROM table
WHERE 'abc' IN (column1, column2, column3)
However, I don't want to return the whole row, only the single value. It possible that the value can found in multiple columns for the same row. Each occurrence should be returned and the end result should be a 1-dimensional list. How do I need to alter my query?
The value that you want returned in each row is the same as the value that you search for, so a statement like this will do:
SELECT 'abc' AS column_name
FROM table
WHERE 'abc' IN (column1, column2, column3)
If you want 'abc' returned once for each occurrence in any of the 3 columns you should use UNION ALL:
SELECT column1 AS column_name
FROM table
WHERE column1 = 'abc'
UNION ALL
SELECT column2 AS column_name
FROM table
WHERE column2 = 'abc'
UNION ALL
SELECT column3 AS column_name
FROM table
WHERE column3 = 'abc'
I want to show column expect column number 1
for example = select * from table; this query will show all column
column1 column2 column3 ...
but my question is how to show all column expect column1, so the result will be like this
column2 column3 ...
I don't want to use select column2, column3 from table because that query not efective if my column more than 3. thank
You can select the column after SELECT:
SELECT * FROM table
* means ALL columns, so:
SELECT columnName, columName2 FROM table
Simple question, i have a query with some WHERE and i need to duplicate it with a change to 1 field to a different value? There is a unique increment ID field as well which I cannot duplicate.
Something along these lines should work. This will give you access to the row that you want to duplicate, and you just select the values to insert for the new row, replacing one of them.
INSERT tblData
( -- Lets pretend Column1 is your key
Column2
, Column3
)
SELECT
Column2
, Column3 + 5 /*Replace this with whatever value is you want*/
FROM tblData
WHERE Column1 = #Id
INSERT INTO table(field1,field2)
SELECT field1*x, field2 /*Replace accordingly*/
FROM table
WHERE key = #Id
Is this possible or do I have to list all the columns?
INSERT INTO table_name (column1, **column3**, column2,...)
VALUES (value1, value2, value3,...)
Do I have to list all the columns in order and have values for each one?
You only have to put the columns you plan on inserting. The only order that needs to match up is the column name and value.
IE: 3 Columns: col1, col2,col3
INSERT INTO TABLE (col1,col2) VALUES(col1value,col2value)
INSERT INTO TABLE (col2,col3) VALUES(col2value,col3value)
INSERT INTO TABLE (col3,col2) VALUES(col3value,col2value)
For your INSERT statement it should be like this:
INSERT INTO table_name (column1, column3, column2,...) VALUES (value1, value3, value2,...)
Since you moved column3, value3 should "match up" with it
You can put your query columns in any order, as long as you specify that order like you are doing above. The actual values you insert as part of the VALUE clause must match the INSERT INTO (x,y,z) part of your query.
Any columns that you do not specify will have a default value inserted. The default value is determined by the DEFAULT value set when the column was created.
Your INSERT can fail if a column has a NOT NULL specification on it and no DEFAULT value, and you do not provide one in the query.
Your column names must correspond to the values that you are inserting. For instance, in your above query, if column1 is a varchar, column3 an int, etc, the values must correspond and be in the exact order for your query to execute successfully.