tags:

views:

54

answers:

3

Hi, I have a MSSQL 2005 database with a lot of records that were added since last backup. I want to make another SQL script that puts result values into string representing INSERT statement that I will save for later use.
Something like:

SELECT 'Insert INTO tabname columns VALUES("+Column1"',')' FROM XY

Or simple example:

Column A,Row1=5

SELECT A+"BLAH" FROM X

should return "BLAH5"

Thank you

A: 

I'm not sure i understand, if you want to build a script (lets say PHP) just run over the records and either print out or to file something like:

echo 'INSERT INTO tablename (field1,field2) VALUES('.$row['field1'].','.$row['field2'].');';

if you want that string in the result directly from the SQL you could use CONCAT:

SELECT CONCAT('INSERT INTO...VALUES(',field1,',',field2,')') FROM yourtable;

Hope that helps...

Ken
A: 

You should really mention what SQL database system you're using.

For MySQL, what you want is the CONCAT function.

SELECT CONCAT('INSERT INTO table (columns) VALUES ("', column1, '");') FROM xy;
Shtééf
A: 

what version of sql? for ms sql, you can use + for concatenation and single quotes for strings

for mysql/oracle, use concat(column, 'string')

davidosomething