Generate SQL insert statements of some records in a table - sql-server

I try to auto generate insert SQL statement from an existing table in SQLServer 2008 but I do not need all record, only a small part of them. --> I thus need to filter the generated inserts. Adding a WHERE clause when generating the insert SQL statements would do the trick but I do not know how to do it.
This article answer to my question partly (SSMS internal generator) :
What is the best way to auto-generate INSERT statements for a SQL Server table?
But it exports all the data of a table. The insert scripts generated are not sorted thus I cannot filter the row I need easily (heavy manual work).
I also tried this stored procedure here (I also had to correct a part of the procedure to make it work with SQLServer 2008 replace char(255) by varchar as explained here)
But it is still not working : I get the following error :
Msg 8169, Level 16, State 2, Line 6
Conversion failed when converting from a character string to uniqueidentifier.
Could you then give me the best way to auto generate SQL Insert in SQL server 2008 from a part of a portion of a table (thus not all the rows of the table) ?

I found a way myself using Excel.
Make needed query including WHERE clause in SSMS
Select all the result
Copy with header
Paste in Excel file here under in 4th row, 1st column
Change in macro output path
Change in cell table name
Launch macro
--> take the file generated and you have a copy of your data ready to be insert again
https://dl.dropboxusercontent.com/u/49776541/GenerateInsert.xlsm

You can use merge syntax to insert data in table based on specific condition
using merge you can also delete and update data in table.you can also do
multiple operation in single sql statement.

There is an easier way to do this, other than going through all the fuss of an excel sheet.
This will return all the data in a table (much like the GUI version) where you right click on the database and select “Tasks” then select “Generate scripts”.
However, unlike the GUI version or the “export to excel” version, with this line of code, you can specify a filter in a “WHERE” clause to return only items for a particular day, or range of days, or any other filter that would normally be used in a “WHERE” clause.
In the code below, I am using 2 simple tables. One is populated with data, the other is not. I want to transfer some or all of the data from table2 to table3. Again, I can filter by date or parts of other columns. (for example… WHERE colB LIKE 'ging%';
This will generate a string of “INSERT” statements preformed in SQL query ready to run.
Note, before running this, switch your output display in SQL server from “Grid” to “Text”.
SELECT 'INSERT', + 'INTO', + 'TestTable3', + '(', + 'colA', + ',', + 'colB', + ',', + 'colDate', + ')', + 'values', + '(', + '''', + CAST(colA AS VARCHAR(10)), + '''', + ',', + '''', + CAST(colB AS VARCHAR(10)), + '''', + ',', + '''', + CAST(DATEADD(DAY, -1, GETDATE()) AS DATE) AS 'colDate', + '''', + ')', + ';'
FROM TestTable2
WHERE colDate LIKE '2018-10-14';
GO
Here is a snippet of what this will return.
Simply copy/paste the results into a new query and run it.
Too easy.

Related

How to use an evaluated expression in SQL IN clause

I have an application that takes a comma separated string for multiple IDs to be used in the 'IN' clause of a SQL query.
SELECT * FROM TABLE WHERE [TABLENAME].[COLUMNNAME]
IN
((SELECT '''' + REPLACE('PARAM(0, Enter ID/IDS. Separate multiple ids by
comma., String)', char(44), ''',''') + ''''))
I have tested that PARAM gets the string entered e.g. 'ID1, ID2' but SELECT/REPLACE does not execute. The statement becomes,
SELECT * FROM TABLE WHERE [TABLENAME].[COLUMNNAME]
IN
((SELECT '''' + REPLACE('ID1,ID2', char(44), ''',''') + ''''))
I am trying to achieve,
SELECT * FROM TABLE WHERE [TABLENAME].[COLUMNNAME]
IN ('ID1', 'ID2')
The query does not return any results/errors. I am confident the corresponding records are in the database I am working with. Not sure how to fix this.
You can't do it like this. The IN operator expects a list of parameters separated by comma, but you supply it with a single parameter that happens to contain a comma delimited string:
If you are working on SQL Server version 2016 or higher, you can use the built in string_split to convert the delimited string into a table.
SELECT *
FROM TABLE
WHERE [TABLENAME].[COLUMNNAME]
IN STRING_SPLIT(#CommaDelimitedString, ',')
For older versions, there are multiple user defined functions you can choose from, my personal favorite is Jeff Moden's DelimitedSplit8K. For more options, read Aaron Bertrand's Split strings the right way – or the next best way.

Dynamically merge the two tables using primary key

I am trying to write a procedure where I can dynamically merge a number of staging+destination tables, each with different column names and primary keys.
Is there a way to merge by extracting the primary key constraint?
I envisioned doing a dynamic SQL statement which looked like this:
'MERGE
' + #DestinationTable + ' Dest
USING
' + #StagingTable ' Staging
ON
' + #JoinConditions + '
WHEN NOT MATCHED THEN
INSERT INTO
Dest
SELECT
*
FROM
Staging
WHEN MATCHED THEN
UPDATE
Dest
SET
' + #UpdateStatement
Where #DestinationTable is a string parameter with the table name, #StagingTable is the same, #JoinConditions is a string which is of the format
Staging.ColumnName = Dest.ColumnName
for each key column, and #UpdateStatement is of the same format except with non-key columns.
It doesn't look pretty, but this is how I foresee it working.
Generating MERGE statements is a staple of the BI/DW space because it is a common requirement.
There are solutions that generate a stored proc and/or merge statements, some will also write the SSIS packages. There
On that note, I'm not going to write a solution here.I downloaded one last time I needed it
You can get a feel for how common this requirement is by searching "generate merge script sql server"
For example
https://github.com/readyroll/generate-sql-merge
https://www.red-gate.com/simple-talk/blogs/generate-a-merge-statement-from-table-structure/
http://billg.sqlteam.com/2011/02/15/generate-merge-statements-from-a-table/

Microsoft SQL Server : CONCAT() with INSERT

I am currently attempting to combine 2 columns into 1 using CONCAT().
I have
SELECT
ApplicationTitle,
ApplicationVersion,
CONCAT(ApplicationTitle,' ',' - ',' ',ApplicationVersion) as ApplicationName
FROM
<DataBaseName>
-- Hid DataBase name due to privacy concerns.
Now this works and gives me the result I would like, but I need to not just see the result but actually insert it into the table as a new column with those values so I can delete ApplicationTitle and ApplicationVersion. Reasoning is when exporting the information (I do not have control over how the information gets exported out), it separates ApplicationTitle from ApplicationVersion (to clarify, they are application names and application versions, but I need it into 1 column with a - divider, e.g. ApplicationTitle = SQL, ApplicationVersion = 4.0, I want ApplicationName = SQL - 4.0). I've looked online but could not find something similar that worked for my current situation due to needing to delete ApplicationTitle and ApplicationVersion after ApplicationName has been populated.
What is the best way to go about doing this. My thought was INSERT INTO command with CONCAT but that doesn't seem to work for me (I'm sure I'm missing something).
Suppose You have a table named TableName and TableName has one column name ColumnName, then you can insert row at TableName by following query:
INSERT INTO TableName
(ColumnName)
SELECT
CONCAT(ApplicationTitle, ' ', ' - ', ' ', ApplicationVersion)
FROM YourTableName
This will create a new table and insert the records how you like, but if you try to run this again with the same table name it will give an error like "table already exists":
SELECT
ApplicationTitle,
ApplicationVersion,
ApplicationName = CONCAT(ApplicationTitle,' ',' - ',' ',ApplicationVersion)
INTO <NewTableName>
FROM <DataBaseName>

SQL Server Export / Import Sequences

What is the best method of exporting SQL Server 2012 Sequence objects from one instance to another instance? Is there something similar to the bcp utility that is used for table data?
We have several hundred to transfer. I need to maintain the next value since we are moving table data as well.
One possibility would be to have your system create SQL scripts that you can then execute on the other system - something like this:
SELECT
'CREATE SEQUENCE ' + seq.name + ' AS ' + t.name +
' START WITH ' + CAST(seq.current_value AS VARCHAR(20)) +
' INCREMENT BY ' + CAST(seq.increment AS VARCHAR(10))
FROM
sys.sequences seq
INNER JOIN
sys.types t ON seq.system_type_id = t.system_type_id
Running this SQL on a given database produces an output that contains CREATE SEQUENCE statement to re-create the sequences, using the current_value as the starting point, and using the defined Increment By value.
Of course, if you need additional options like CYCLE or NO CYCLE etc. - everything that's contained in the sys.sequences catalog view can be scripted out as needed :-)

How to create UPDATE from exported data?

I'd like to get data from one database..table into an UPDATE script that can be used with another database..table. Rather than doing export from DB1 into DB2, I need to run an UPDATE script against DB2.
If the above is not possible, is there a way to generate the UPDATE syntax from DB1..table1:
col1 = value1,
col2 = value2,
col3 = value3,
...
--- EDIT ---
Looking through the answers, there's an assumption that DB1 is available at the same time that DB2 is available. This is not the case. Each database will know nothing of the other. The two servers/databases will not be available/accessible at the same time.
Is it possible to script the table data into a flat file? Not sure how easy that will be to then get into an UPDATE statement.
Using a linked server and an update statement will really be your easiest solution as stated above, but I do understand that sometimes that isn't possible. The following is an example of dynamically building update statements. I am assuming there is no chance of SQL Injection from the "SourceData" table. If there is that possibility then you will need to use the same technique to build statements that use sp_executesql and parameters.
SELECT 'UPDATE UpdateTable ' +
' SET FieldToUpdate1 = ''' + SourceData.DataToUpdate1 + '''' +
' , FieldToUpdate2 = ' + CAST(SourceData.DataToUpdate2 AS varchar) +
' WHERE UpdateTable.PrimaryKeyField1 = ' + CAST(SourceData.PrimaryKey1 AS varchar) +
' AND UpdateTable.PrimaryKeyField2 = ''' + SourceData.PrimaryKey2 + ''''
FROM SourceData
Also here is a link to a blog I wrote on Generating multiple SQL statements from a query. It's a bit more simplistic than the type of statement you are trying to create, but it should give you an idea. Also here is an article I wrote on using Single Quotation Marks in SQL. Other than that you can go onto Google and search for "SQL Server Dynamic SQL" and you will get hundreds of blogs, articles, forum entries etc on the subject.
Your question needs a little more clarification to be completely understand what you are trying to accomplish, but assuming the databases are on the same server, then you should be able to do something like this using UPDATE and JOIN:
UPDATE a
SET col1 = value1, col2 = value2
FROM database1.schema.table a
JOIN database2.schema.table b
ON a.primaryKey = b.primaryKey
Alternatively, if they are on different servers, you could setup a linked server and it should work similarly.
I think you still want to INSERT from one table into another table of another database. You can use INSERT INTO..SELECT
INSERT INTO DB2.dbo.TableName(Col1, Col2, Col3) -- specify columns
SELECT Col1, Col2, Col3
FROM DB1.dbo.TableName
Assuming dbo is the schema used.
Are both databases on the same SQL-Server? In that case, use fully-qualified table names. I.e.:
Update Database1.Schema.Table
SET ...
FROM
Database2.Schema.Table
If they're not on the same server, then you can use linked servers.
I'm not sure of the SQL server syntax but you can do something like this to generate the update statement.
SELECT 'UPDATE mytable SET col1=' || col1 || ' WHERE pk=' primaryKey ||';' FROM mytable;
Obviously you'll need to escape quotes, etc. depending on the value types.
I assume this is because you can't do a normal UPDATE from a SELECT?

Resources