开发者

Joining SQL result with string

开发者 https://www.devze.com 2022-12-18 14:22 出处:网络
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 sa

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 tabn开发者_运维百科ame columns VALUES("+Column1"',')' FROM XY

Or simple example:

Column A,Row1=5

SELECT A+"BLAH" FROM X

should return "BLAH5"

Thank you


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...


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;


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号