Select statement result change as insert statement values

  • Hi All,

    Looking for following results. Can you please help in this.

    Original

    1 A US

    Expected result

    1,'A','US'

     

    create table #tbl_Test (ID int, Name varchar(20),City varchar(20))

    insert into #tbl_Test values (1,'A','US')

    Select * from #tbl_Test

    Expected result
    1,'A','US'
  • SELECT ID
    ,Name = CONCAT ('''', Name, '''')
    ,City = CONCAT ('''', City, '''')
    FROM #tbl_Test;

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Phil Parkin wrote:

    SELECT ID
    ,Name = CONCAT ('''', Name, '''')
    ,City = CONCAT ('''', City, '''')
    FROM #tbl_Test;

    Or even simpler if you have a lot of tables to do it for:

    DECLARE @Quote varchar(100) = ''''
    SELECT ID
    ,Name = CONCAT (@Quote, Name, @Quote)
    ,City = CONCAT (@Quote, City, @Quote)
    FROM #tbl_Test;
  • Sorry, missed the comma.

    Result should be - 1 ,'A','US'

    insert into #tbl_Test values (1 ,'A','US')

     

    • This reply was modified 1 year, 1 month ago by  Saran.
  • Do you want a single-cell result, rather than multiple columns?

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • I just need first line or row from the table. That I am going to copy and put it in insert statement to insert to table.

  • If you look at this post there is a stored procedure that will generate the inserts for any table: https://www.sqlservercentral.com/forums/topic/generate-script-for-table-contents#post-4184688

    Just install it and call it:

    EXEC INFGenerateInserts @table_name='#tbl_Test';
  • Thank you Jonathan AC Roberts.

    It is working amazing.

     

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply