November 2, 2012 at 2:37 am
CompanyNameEmpFNameEmpLName
AA1s
BA2S1
CC1S2
DD1S3
This is my Resultant set
How can I want to save these Result into another table
November 2, 2012 at 2:47 am
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
November 2, 2012 at 2:54 am
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
November 2, 2012 at 2:54 am
The Result not saved into table1
The result of table was get by executing some queries
November 2, 2012 at 2:59 am
Please provide your SQL.
November 2, 2012 at 3:00 am
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
November 2, 2012 at 3:08 am
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
November 2, 2012 at 3:10 am
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.
November 2, 2012 at 3:23 am
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.
November 2, 2012 at 3:25 am
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
November 2, 2012 at 3:42 am
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'.
November 2, 2012 at 3:53 am
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
November 2, 2012 at 3:57 am
Thanks for your Suggestions
its working fine
November 2, 2012 at 3:59 am
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