I am trying to write a database script to insert the data from a select.
but now there is an edit in database and I need to insert the same data but in two tables instead of one with foreign key.
Tables:
Membership: Id, Role, AccountName,...
MembershipMapper: Id, MembershipId, Role
I wrote this script:
INSERT INTO [server1].[dbo].[Memberships] ([Role], [AccountName] .....)
SELECT
[Role],
[AccountName],
......
FROM
[server2].[dbo].[Memberships]
but now the Role is in another table. How to insert it? Any ideas?
Possibly this?
INSERT INTO [server1].[dbo].[Memberships] ([Role], [AccountName] .....)
OUTPUT [ID], [Role] INTO MembershipMapper (MembershipId, Role)
SELECT
[Role],
[AccountName],
......
FROM
[server2].[dbo].[Memberships]
You'll probably need a JOIN:
INSERT INTO [server1].[dbo].[Memberships] ([Role], [AccountName] .....)
SELECT
s.[Role],
m.[AccountName],
......
FROM
[server2].[dbo].[Memberships] m
INNER JOIN
[server2].[dbo].[SomeOtherTable] s ON m.ID = s.ID -- or whatever links the two tables.....
Try something like this
INSERT INTO [server1].[dbo].[Memberships]
([Role]
,[AccountName]
.....)
SELECT memmap.[Role]
,mem.[AccountName]
......
FROM [server2].[dbo].[Memberships] AS mem
JOIN [server2].[dbo].[MembershipMapper] AS memmap
ON mem.id = memmap.MembershipId
Related
I want to insert the data from one table to another. The condition is if the first table having only record then only that record I need to insert into the another table.
I am trying Query :
INSERT INTO Customer_Map_Address(CustomerID, AddressID, IsBillingAddress)
SELECT [CustomerID], [ID], CAST('FALSE' as BIT) AS IsBillingAddress
FROM CustomerAddress Group By CustomerID, ID
having COUNT(CustomerID) = 1
It is Grouped by CustomerID and ID hence getting wrong output. I want to group only by CustomerID.
Thanks.
INSERT INTO dbo.Customer_Map_Address (CustomerID, AddressID, IsBillingAddress)
SELECT [CustomerID], MAX([Id]), 0 AS IsBillingAddress
FROM dbo.CustomerAddress
GROUP BY CustomerID
HAVING COUNT(*) = 1
I am running the query :
select
[temp_table_excel_insert_for_join].person_id,
[temp_table_excel_insert_for_join].city,
city.city_id
from
temp_table_excel_insert_for_join, city
where
temp_table_excel_insert_for_join.city = city.city
without problem now I want to have all these columns as a new table so I used
create table mytable as
( select
[temp_table_excel_insert_for_join].person_id,
[temp_table_excel_insert_for_join].city,
city.city_id
from
temp_table_excel_insert_for_join, city
where
temp_table_excel_insert_for_join.city=city.city)
but it did not work for me what should I do to make it happen? I don't want to create a view I want to have a table.but I am not familiar if I should do a left join or other things
If your goal is to create a new table mytable with the columns person_id, city, city_id then use the select ... into syntax:
select
[temp_table_excel_insert_for_join].person_id,
[temp_table_excel_insert_for_join].city,
city.city_id
into
mytable
from
temp_table_excel_insert_for_join
inner join
city on temp_table_excel_insert_for_join.city = city.city
Note that this will fail if you run it more than once as the table already would exist and you would have to drop it first.
See the documentation for more information on the into clause
Try this
select [temp_table_excel_insert_for_join].person_id
, [temp_table_excel_insert_for_join].city
,city.city_id
into mytable
from temp_table_excel_insert_for_join inner join city on temp_table_excel_insert_for_join.city=city.city)
Try separating the two operations. First:
CREATE TABLE MyTable ( etc)
Then
INSERT INTO MyTable ( <List of columns to insert> )
SELECT <Columns to insert>
FROM etc
WHERE etc
This works if you remove the brackets after your AS clause - at least in SQL Lite. So you can just write:
CREATE TABLE mytable AS
SELECT
[temp_table_excel_insert_for_join].person_id,
[temp_table_excel_insert_for_join].city,
city.city_id
FROM
temp_table_excel_insert_for_join, city
WHERE
temp_table_excel_insert_for_join.city=city.city;
I've tried searching for the answer to this one to no avail. There is no good logic behind the way this was setup. The guy does not know what he's doing, but it's what I have to work with (long story).
I'm using SQL Server 2008R2 I need to take records from one table and transfer the data to 4 separate tables all with a one to one relationship (I know - not smart). I need to get the value from the Identity field in the first table the data is inserted into, then populate the other 3 tables with the same ID and disperse the data accordingly. for example:
OldTable: Field1, Field2, Field3, Field4
NewTable1: Identity field, Field1
NewTable2: ID, Field2
NewTable3: ID, Field3
NewTable4: ID, Field4
I'd like to handle this in a stored procedure. I'd like to do a loop, but I read that loops in SQL are inadvisable.
Loop moving through each record in OldTable... (??)
INSERT INTO NewTable1
(Field1)
Select Field1 from OldTable
INSERT INTO NewTable2
(ID, Field2)
Select SCOPE_IDENTITY?, Field2 From OldTable Where OldTable.ID = ??
etc for other 2 tables
Loop to next record in OldTable
I am not sure how to use SCOPE_IDENTITY, but I have a feeling this will be involved in how I accomplish this.
Also, I'm probably going to need to setup a trigger for whenever a new record is created in NewTable1. I know, it's insanity, but I can't do anything about it, just have to work around it.
So, I need to know
1: the best way to initially populate the tables
2: how to make triggers for new records
The solution to 1 might involve 2.
Please help!
You can use the output clause of the merge statement to get a mapping between the existing primary key in OldTable and the newly generated identity ID in NewTable1.
-- Temp table to hold the mapping between OldID and ID
create table #ID
(
OldID int primary key,
ID int
);
-- Add rows to NewTable1 and capture the ID's in #ID
merge NewTable1 as T
using OldTable as S
on 1 = 0
when not matched by target then
insert(Field1) values(S.Field1)
output S.ID, inserted.ID into #ID(OldID, ID);
-- Add rows to NewTable2 using #ID to get the correct value for each row
insert into NewTable2(ID, Field2)
select I.ID, O.Field2
from #ID as I
inner join OldTable as O
on I.OldID = O.ID
insert into NewTable3(ID, Field3)
select I.ID, O.Field3
from #ID as I
inner join OldTable as O
on I.OldID = O.ID
insert into NewTable4(ID, Field4)
select I.ID, O.Field4
from #ID as I
inner join OldTable as O
on I.OldID = O.ID
drop table #ID;
SQL Fiddle
See also Using merge..output to get mapping between source.id and target.id
How about using the OUTPUT clause of the insert statement? Assuming that Field1 is a unique key on the OldTable...
Declare #IDinserted table(ID int, Field1 varchar(255));
Insert Into NewTable1(Field1)
Output inserted.ID, inserted.Field1 into #IDinserted
Select OldID, Field1 from OldTable;
Insert Into NewTable2(RowID, Field2)
Select i.ID, o.#Field2
from #IDinserted i Inner Join OldTable o
on i.Field1=o.Field1;
Is it possible to insert data from select statement into a a dynamically created table? I will not know how many columns are there in select statement until I run the query. It has to create the appropriate number of columns at run time.
Thank you,
Smith
Just use the SELECT INTO syntax:
SELECT *
INTO NewTable
FROM MyTable
WHERE ...
The new table NewTable will be created with the same columns as your select statement.
You can use a CTAS pattern for this
USE AdventureWorks
GO
----Create new table and insert into table using SELECT INSERT
SELECT FirstName, LastName
INTO TestTable
FROM Person.Contact
WHERE EmailPromotion = 2
----Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
----Clean Up Database
DROP TABLE TestTable
GO
Have a look at This Article on CTAS
-- This creates temporary table
select *
into #newtable
from YourTable
select * from #newtable
-- This creates physical table in the DB
select *
into newtable
from YourTable
select * from newtable
I have several values in a temp table called #tempIQ and I want to insert into a table called IQGroups using the same Group identifier. Assuming everyone has a unique IQ:
create table #tempIQ
(
id int
)
declare #GroupIDas int
set #GroupID=1001
select iq from #tempIQ
1,2,86,99,101,165,180,201
I want to insert these ids from the temp table into a grouping called IQGroups but am having difficulty finding a simple solution.
-- now try and insert all the iqs for a group into the IQGroups table from the #tempIQ table.
insert into IQGroups (GroupID, IQ) values (#GroupID, #tempiQ.iq)
Try this:
INSERT INTO IQGroups (GroupID, IQ)
SELECT #GroupID, IQ
FROM #tempIQ
Try using the SELECT statement.
INSERT INTO IQGroups (GroupID, IQ)
SELECT #GroupID, iq
FROM #tempIQ
This is the standard way to select multiple rows.
this is another way to do this,
select id, 1001 as GroupID
into IQGroups
from #tempIQ