How to Save Resultent Set into Anoher table

  • CompanyNameEmpFNameEmpLName

    AA1s

    BA2S1

    CC1S2

    DD1S3

    This is my Resultant set

    How can I want to save these Result into another table

  • Build an insert statement around the T-SQL you used to generate the result set, like below

    INSERT INTO table2 (Col1, Col2, Col3)

    SELECT Col1, Col2, Col3 FROM table1

  • if already table structure in there in database you fellow below logic..

    INSERT INTO table2 (Col1, Col2, Col3)

    SELECT Col1, Col2, Col3 FROM table1

    if table is not there in data that fimt

    follw below logic..

    SELECT Col1, Col2, Col3 into table2 FROM table1

  • The Result not saved into table1

    The result of table was get by executing some queries

  • Please provide your SQL.

  • then also it will work lets say

    insert into table2

    select * from

    (

    select a.*

    from a

    inner join b on a.id=b.id

    inner join c on b.id=c.id

    where a.name='ee'

    )a

  • Actually the resulte can get by using

    Union all statement

    from a table (its a parent table ) I want to save the result into another table

  • Then you need to build an insert statement around your select.

    Please provide the DDL of all the tables in question, along which which columns from each should be stored into the table.

  • How can we Insert into anoher table

    select [Company Name] as CompanyName,ISNULL([Executive First Name 10],'') As ExecutiveFName,

    ISNULL([Executive Last Name 10],'')As ExecutiveLName,[Executive Biography 10] from CompaniesExecutives

    Insert into TblExecutives(CompanyName,ExeFName,ExeLastName)values(CompanyName,ExecutiveFName,ExecutiveLName)

    this is my sql query

    but i got an error as

    The name "CompanyName" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

  • Insert should be the first line, not the last, check my sample code.

    Insert into TblExecutives(CompanyName,ExeFName,ExeLastName)

    select [Company Name] as CompanyName,ISNULL([Executive First Name 10],'') As ExecutiveFName,

    ISNULL([Executive Last Name 10],'')As ExecutiveLName from CompaniesExecutives

  • Its working only single

    but I want to save using union all statement

    Insert into TblExecutives(CompanyName,ExeFName,ExeLastName,Biograpy)

    select [Company Name] as CompanyName, ISNULL([Executive First Name 1],'') As ExecutiveFName,

    ISNULL([Executive Last Name 1],'')As ExecutiveLName,[Executive Biography 1]as Biography from CompaniesExecutives

    union all

    Insert into TblExecutives(CompanyName,ExeFName,ExeLastName,Biograpy)

    select [Company Name] as CompanyName, ISNULL([Executive First Name 2],'') As ExecutiveFName,

    ISNULL([Executive Last Name 2],'')As ExecutiveLName,[Executive Biography 2]as Biography from CompaniesExecutives

    this is my query when execute gets an error as

    Incorrect syntax near the keyword 'Insert'.

  • You have two insert statements in your code - you can not "UNION ALL" two insert statements.

    What you want (I think) is to insert into a new table a query that happens to contain a UNION ALL statement.

    You need the following construct:

    INSERT INTO [TheTargetTable]

    VALUES ([either actual values or a select statement or SP, etc])

    In your case, the VALUES are a SELECT statement that happens to use UNION ALL ...

    so:

    INSERT INTO TblExecutives(CompanyName,ExeFName,ExeLastName,Biograpy)

    (

    SELECT [Company Name] AS CompanyName

    , ISNULL([Executive First Name 1],'') AS ExecutiveFName

    , ISNULL([Executive Last Name 1],'') AS ExecutiveLName

    , [Executive Biography 1] AS Biography

    FROM CompaniesExecutives

    UNION ALL

    SELECT [Company Name] AS CompanyName

    , ISNULL([Executive First Name 2],'') As ExecutiveFName

    , ISNULL([Executive Last Name 2],'') As ExecutiveLName

    , [Executive Biography 2] AS Biography

    FROM CompaniesExecutives

    )

    note: when posting code, try to use the code tags to have it displayed in a pretty manner.

    Enter [c o d e] your code here [/ c o d e] .. (without spaces in code obviously) - look on your left of the text post when you post a reply and you will find the tags to format your text (IFCode Shortcuts))

    HTH,

    B

  • Thanks for your Suggestions

    its working fine

  • As a side note, please follow the second link in my signature on posting code and data for the best, help. If the intial post contains this information you are more likely to get an answer quicker than having to work out what the problem is.

Viewing 14 posts - 1 through 13 (of 13 total)

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