I am trying to insert data into multiple columns of a table with specific values. However one varchar column will have its data value coming from another table select statement placed inside a quotes.
I need it placed inside without having the sub select statement interpreted as part of the string. Below is my query;
INSERT INTO Table1
(column1,
column2,
column3,
column4)
VALUES (484640,
4,
1,
'<HTML><Head></Head><Body>Upload results</Body></HTML>')
Currently your select query will be just treated like a string since it is enclosed within single quotes. The query will be inserted into the column, not the result.
Here is the correct way:
INSERT INTO Table1
(column1,
column2,
column3,
column4)
SELECT DISTINCT 484640,
4,
1,
'<HTML><Head></Head><Body><a href="cgroup-histstatus.aspx?Staging_PKID='
+ cast(column_1 as varchar(50))
+ '">Upload results</a></Body></HTML>'
FROM Table_2
If you want the values from column_1 to be enclosed within single quotes then replace '+ column_1 +' with '''+ column_1 +'''.
This works even when select query returns more than one record.
Related
How to create table in presto from a select query that has comma-separated values inside the column itself.
e.g
select statement has values look like
Column1
column2
column3
1
2
abc,xyz
below is the create statement for table
CREATE TABLE resultTable
WITH (format = 'TEXTFILE',textfile_field_separator = ',')
AS
select Column1 ,column2 column3 from sourceTable
but this ends up with missing placing fields which have comma separated values
i am doing some inserts with some of my tables and all fields but a few need to be inserted or updated. Like here.
Is there a quick way to get the columns of a table returned as a string so i can copy them into my sql query?
For example i have a table:
create table myTable1
(
column1 integer not null,
column2 integer not null ,
column3 integer not null,
column4 integer not null,
column5 integer not null,
...a lot more fields...
)
And later i want to insert something from another table myTable2 that has the same fields (dont question my ways and why i have two identical tables).
INSERT INTO myTable SELECT column1, column2, column3, column4, ...
FROM table_source
But the tables have so many fields it is cumbersome to write them donw manualy and it would be faster to have a string where i can just delete the column names i dont need. Is there a nice query that ouputs "column1, column2, column3, column4, ..." so i dont have to write that myself and can copy tha into my query?
Found the answer quickly.
SELECT table_catalog, string_agg(column_name, ', ')
FROM information_schema.columns
WHERE table_schema = 'mySchema'
AND table_name = 'myTable' GROUP BY 1;
This query does the trick for me.
Is it possible for an INSERT query to subtract 2 values you have entered to create a 3rd value that can then be inserted into a table - if that makes sense...
e.g.
INSERT INTO table1 (column1, column2, column3)
VALUES ('50', '25', column1 - column2)
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', column1 - column2) from table1 where conditions)
This is a sample query! hope it helps!
Convoluted:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
(select 50 as column1,
25 as column2
) t
Since you can't reference other columns from the same SELECT clause, you have to do it as a subquery. I've also switched to using int literals rather than strings, because I can't make subtraction make sense in my head otherwise.
You could also do it using a Table Value Constructor:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
( VALUES (50, 25)
) AS t (column1, column2);
As indicated in my comment though, if the relationship should always hold, I'd build table1 as:
CREATE TABLE table1 (
column1 int not null,
column2 int not null,
column3 as column1 - column2
--More columns
)
Because that way, the column3 value is always correct.
You can create function that subtracts values and use this function in insert. This is the right way to do such things:
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', your_function() ) from table1 where conditions)
/
Using the "INSERT INTO" would do this:
INSERT INTO Table1Name (column1, column2, column3,)
(select 'X', 'Y', X - Y as Z)
Here is a link to SQL Authority with more examples of INSERT INTO
Another method would to be add a trigger to the table, where on insert of data, the third column would be updated with the difference of the first two columns.
I'm trying to write a sql query that can copy specific columns from a table and insert it in the same table + extra information for the other columns
Simply copying certain information would be something like this:
INSERT INTO table (column1, column2)
SELECT column1, column2
FROM table
WHERE columnx = 'some value'
But I need to also insert some new information in column3. How can I do that?
I have the information that will go in "column3", I don't have to get it from an other table or source.
This is for a repeat appointment where basically all the information is the same except for date, planner and appointment_id.
If you know what the values are . . .
INSERT INTO table (column1, column2, column3)
SELECT column1, column2, <value for column3>
FROM table
WHERE columnx = 'some value'
I want to transfer all data from one table into another with the following code:
INSERT INTO tblpremier
SELECT * INTO #TempTable
FROM dbo.IntermediateTable
ALTER TABLE #TempTable
DROP COLUMN id
SELECT * FROM #TempTable
And I get an error
Incorrect syntax near the keyword 'INTO'
at the second line of this code. Any help?.
What you've written doesn't make any sense. The first "statement" is this:
INSERT INTO tblpremier
SELECT * INTO #TempTable
FROM dbo.IntermediateTable
Now are you inserting into #temptable, or tblpremier? I'm guessing you wanted to perform all these operations and then insert into tblpremier - in which case split it into separate statements. I'm guessing you wanted to do:
SELECT * INTO #TempTable
FROM dbo.IntermediateTable
ALTER TABLE #TempTable
DROP COLUMN id
INSERT INTO tblpremier
SELECT * FROM #TempTable
But rather than need #Temptable which is the same as IntermediateTable minus the ID column, why not just select the correct columns you need from IntermediateTable in the first place rather than using *?
Edit:
Here's what I meant. Write the insert statement so you've got all the column names specified, and don't include the ID column. You'll get all new ID numbers on the copy of the table.
INSERT INTO tblpremier (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM IntermediateTable
However, if you wanted to to keep the same ID numbers in the copy of the table, and the columns are in the same order on both intermediate table and tblpremier, then you could write:
SET IDENTITY_INSERT tblpremier ON
INSERT INTO tblpremier
SELECT *
FROM IntermediateTable
SET IDENTITY_INSERT tblpremier OFF
But you would still need to watch out for trying to insert duplicate IDs if tblpremier isn't empty at first.
You cannot use both INSERT INTO TABLE SELECT FROM and SELECT INTO at the same time. You should break that statement into
INSERT INTO tblpremier
SELECT * FROM dbo.IntermediateTable
SELECT * INTO #TempTable
FROM dbo.IntermediateTable
ALTER TABLE #TempTable
DROP COLUMN id
SELECT * FROM #TempTable
You have both a SELECT INTO and INSERT INTO in the same statement.