I've selected some data from a table it gives some rows in as result. Now, I want to generate insert statements from result data in SQL Server.
Please suggest me any solutions.
If the destination table is a new table,
you may use SQL SELECT INTO Statement.
We can copy all columns into the new table:
SELECT *
INTO newtable [IN externaldb]
FROM table1;
Or we can copy only the columns we want into the new table:
SELECT column_name(s)
INTO newtable [IN externaldb]
FROM table1;
The new table will be created with the column-names and types as defined in the SELECT statement. You can apply new names using the AS clause.
Like this,
SELECT 'insert into tabledestination (col1destination,col2destination)
values (' + col1source + ',' + col2source + ')'
FROM tablesource;
Related
I have a select query that returns a single column. Is there a way in sqlite to create a new table using the results as column names?
I tried this but it did not work.
CREATE TABLE newTable (SELECT nameCol FROM oldTable);
SQLite does not support dynamic SQL so this is not possible.
The best that you can do is construct the SQL statement that you can use to create the table by using your preferred programming language:
SELECT 'CREATE TABLE newTable (' ||
(SELECT GROUP_CONCAT(nameCol) FROM oldTable) ||
');' AS sql;
The above query returns 1 row with 1 column with a string like:
CREATE TABLE newTable (column1,column2,column3);
See a simplified demo.
This question already has answers here:
How to create table using select query in SQL Server?
(4 answers)
Closed 9 years ago.
I want to create a table from select query result in SQL Server, I tried
create table temp AS select.....
but I got an error
Incorrect syntax near the keyword 'AS'
Use following syntax to create new table from old table in SQL server 2008
Select * into new_table from old_table
use SELECT...INTO
The SELECT INTO statement creates a new table and populates it with
the result set of the SELECT statement. SELECT INTO can be used to
combine data from several tables or views into one table. It can also
be used to create a new table that contains data selected from a
linked server.
Example,
SELECT col1, col2 INTO #a -- <<== creates temporary table
FROM tablename
Inserting Rows by Using SELECT INTO
Standard Syntax,
SELECT col1, ....., col# -- <<== select as many columns as you want
INTO [New tableName]
FROM [Source Table Name]
Please be careful,
MSSQL: "SELECT * INTO NewTable FROM OldTable"
is not always the same as
MYSQL: "create table temp AS select.."
I think that there are occasions when this (in MSSQL)
does not guarantee that all the fields in the new table are of the same type as the old.
For example :
create table oldTable (field1 varchar(10), field2 integer, field3 float)
insert into oldTable (field1,field2,field3) values ('1', 1, 1)
select top 1 * into newTable from oldTable
does not always yield:
create table newTable (field1 varchar(10), field2 integer, field3 float)
but may be:
create table newTable (field1 varchar(10), field2 integer, field3 integer)
Please try:
SELECT * INTO NewTable FROM OldTable
Try using SELECT INTO....
SELECT ....
INTO TABLE_NAME(table you want to create)
FROM source_table
Select [Column Name] into [New Table] from [Source Table]
I have a table named sampletable
and it has 10000 rows and i want to generate script (Insert query with inserted data) for this table based on certain conditions.
That is i need the insert query with data for certain conditions
like select * from sampletable where id=10 something like that
The select query will results 100 rows only so i need to get only this 100 rows as insert query statement with inserted values
I had tried
Sql Management Studio Generate Script methods but it results all rows.
I don't know how to do this please help me to solve this .
My purpose is i need to take backup of the data (Only the Specific condition data not all)
You can generate an insert query by building a string, for example:
select 'insert into dbo.YourTable (str_col, int_col) values (' +
isnull('''' + strcol + '''', 'NULL') + ', ' +
isnull(cast(int_col as varchar(max)), 'NULL') + ');'
from SampleTable
where id=10
If it's a one-off job, you can use Tasks->Export Data and friends instead of manually crafting SQL inserts.
I have two tables:
Table 1 (Name, (...), ProductID)
Table 2 (Customer Name, ProductID)
Sample Table 1
Test1 | 1
Sample Table 2:
Customer1 | 1
Customer2 | 1
The table schema is fixed and I cannot change it and I cannot create additional views, etc.
With SQL Server Integration Services I have to create a new table. Within this new table I need to have one column with the customers like Customer1; Customer2 for Test1.
I know that I could do it with COALESCE, but I have no idea how to do that within SSIS. Which transformation should I use, and how?
Update
Here is the sample for COALESCE:
DECLARE #names VARCHAR(150)
SELECT #names = COALESCE(#names + '; ', '') + [CustomerName]
FROM Table2
SELECT #names
How to insert this snippet into one new SELECT * FROM Table1?
Take your OLEDB source and use a SQL query for that source.
SELECT T1.Name, T2.Customer
FROM [TABLE 1] AS T1
INNER JOIN [Table 2] as T2
ON T1.ProductID = T2.ProductID
Then use a data flow to move that to your OLEDB destination. No need to get fancy with SSIS on this if you can easily make the database engine handle it.
Your best, and most performant, solution would be to use a SQL Query as your Source, instead of the raw tables. Then you can do the COALESCE (or Concatenation) in the SQL query and it will be passed through the SSIS data pipe.
Ok, I've found the solution:
SELECT [All_My_Other_Fields], STUFF(
(SELECT '; ' + [CustomerName]
FROM Table2
WHERE Table2.ProductID=Table1.ProductID
FOR XML PATH (''))
, 1, 1, '') AS Customers
FROM Table1
Thank you all for your help!
I have a table that has several columns of textual data. The goal is to concatenate those columns into a single different column in the same table and same row.
What is the SQL Server query syntax that would allow me to do this?
Something like this:
UPDATE myTable SET X = Y + Z
Do you absolutely have to duplicate your data? If one of the column values changes, you will have to update the concatenated value.
A computed column:
alter table dbo.MyTable add ConcatenatedColumn = ColumnA + ColumnB
Or a view:
create view dbo.MyView as
select ColumnA, ColumnB, ColumnA + ColumnB as 'ConcatenatedColumn'
from dbo.MyTable
Now you can update ColumnA or ColumnB, and ConcatenatedColumn will always be in sync. If that's the behaviour you need, of course.
Might be misunderstanding but:
Alter table myTable add combinedColumn Varchar(1000);
Update myTable set combinedColumn = textField1 + textField2;
select
textfield1 + textfield2 + ... + textfieldN as conc_text,
otherfield1,
otherfield2,
...
otherfieldN
from
mytable