T-SQL Insert Column from Other Table - sql

Hello I am trying to insert values from one table column to another table. I am using SQL Server 2008 R2. Here is example:
Table 1
Declare #Table1 table (file varchar(15), type int, partiesid int)
Table 2
Declare #Table2 table (file varchar(15), ....many other columns)
I would like to insert file from Table 2 into Table 1 as well as some other static values.
So select * from table1 would end up looking like this:
File Type PartiesID
GW100 1 555
GW101 1 555
GW103 1 555
GW104 1 555
where the GW100, GW101, etc come from table2 and the 1 and 555 are static for every row.
I tried insert into table1 (file,type,partiesid) values (select file from table2,1,555) but that doesn't work. Is there a way where it will just insert a row for each unique file and also insert the static fields of 1 and 555 as separate columns?
Thanks!

Insert into #table2
(
file,
type,
partiesid,
...(other columns for which you need to give static values)
)
select
file,
type,
partiesid,
...(static values for columns)
from #table1

You can try the below query:
USE dbName
GO
INSERT INTO table2 (column_name(s))
SELECT column_name(s) FROM table1;
GO

Related

Insert from select on same table and store old and new identity values in separate table

Temporary table:
declare #Temp_Table table
(
newSID int,
oldSID int
)
Database table: Solutions
Solutions:
* 1 solution1 111
* 2 solution2 111
* 3 solution3 111
After insert,
* 1 solution1 111
* 2 solution2 111
* 3 solution3 111
* 4 solution1 222
* 5 solution2 222
* 6 solution3 222
temp table Expected
oldsID NewSID
* 1 4
* 2 5
* 3 6
This table has SID (identity), SName and cnumber.
Now I want to select some rows from the Solutions table and insert their values into same table.
When inserting each row I want to store the old identity value and new identity value in the temporary table (#Temp_Table).
Please help me with this.
The trick is to use merge instead of a regular insert into..select, since with merge you can use data from both the source and the target in the output clause.
First, create and populate sample table (Please save us this step in your future questions):
CREATE TABLE Solutions
(
SolutionID int identity (1,1),
SolutionName varchar(10),
ClientNumber int
)
INSERT INTO Solutions (SolutionName, ClientNumber) VALUES
('solution1', 111),
('solution2', 111),
('solution3', 111)
Then, declare the mapping table:
DECLARE #Temp_MasterSolutionsTable AS TABLE
(
newSolutionID int,
oldSolutionID int
)
Next, Copy the records you want:
MERGE INTO Solutions USING
(
SELECT SolutionID, SolutionName, ClientNumber
FROM Solutions
--WHERE CONDITION -- I'm guessing you will need a where clause here
) AS s ON 1 = 0 -- Always not matched
WHEN NOT MATCHED THEN
INSERT (SolutionName, ClientNumber)
VALUES (s.SolutionName, s.ClientNumber)
-- and here is where the magic happens
OUTPUT Inserted.SolutionID, s.SolutionID
INTO #Temp_MasterSolutionsTable (newSolutionID, oldSolutionID);
See a live demo on rextester.

Why seed value not working fine?

I'm using SQL Server 2012, and I have a primary key column of type bigint.
Sometimes on new insertions it takes a huge jump of 1000 or 10000 for new primary key.
For example:
ID
--
1
2
3
4
5
6
7
8
9
10001
10002
Why does this happen? Is this a bug?
This is the behaviour of SQL Server 2012.
To fix this issue, you need to make sure, you add a NO CACHE option in sequence creation / properties like this.
create sequence Sequence1
as int
start with 1
increment by 1
no cache
go
create table Table1
(
id int primary key,
col1 varchar(50)
)
go
create trigger Trigger1
on Table1
instead of insert
as
insert Table1
(ID, col1)
select next value for Sequence1
, col1
from inserted
go
insert Table1 (col1) values ('row1');
insert Table1 (col1) values ('row2');
insert Table1 (col1) values ('row3');
select *
from Table1
Hope this helps..

Extract Row ID from Table - Insert Then Select

Is it Possible to extract the ID of the record being inserted in a table at the time of inserting dat particular record into that table ??? Reference to Sql Server
Read about INSERT with OUTPUT. This is in my experience the easiest way of achieving an atomic INSERT outputting an inserted value.
Example, assuming that Table contains an auto-incremented field named ID:
DECLARE #outputResult TABLE (ID BIGINT)
INSERT INTO Table
(
Field1,
Field2
)
OUPUT INSERTED.ID INTO #outputResult
VALUES
(
....
)
SELECT TOP 1 ID FROM #outputResult
You can select the ID afterwards with
SELECT ##IDENTITY
or
SELECT SCOPE_IDENTITY()

SQL Server : Insert two stored procedure results into one table

I have two stored procedure sp1 and sp2
sp1 returns results
value1
------
1
2
3
4
5
sp2 returns results
value2
------
4
5
6
7
8
I have a table called test that has two columns value1 and value2, how to insert sp1 result in value1 column and sp2 result in value2 column in test table?
I am using this
insert into test
exec [sp1], exec [sp2]
It is causing an error but it is working for single value please click following link
The only way I can think of is the following:
declare #t1 as table (id int identity(1,1), val int);
declare #t2 as table (id int identity(1,1), val int);
insert into #t1 (val)
exec sp1;
insert into #t2 (val)
exec sp2;
insert into test
select t1.val, t2.val
from #t1 t1 full outer join
#t2 t2
on t1.id = t2.id
If these are functions you can do the following, pseudocode:
first_val = select sp1();
second_val = select sp2();
insert into test values (first_val,second_val);
This may work as well:
insert into test values (select sp1(),select sp2());
You cannot use procedures as there is no way to return a value from a procedure.

Order By for NVARCHAR column in SQL Server

I have a NVARCHAR(255) column in a SQL Server 2005 that contain either letters or numbers.
Declare #Temp Table(Name NVARCHAR(255))
Insert Into #Temp Values('1')
Insert Into #Temp Values('2')
Insert Into #Temp Values('3')
Insert Into #Temp Values('10')
Insert Into #Temp Values('aaaa')
Insert Into #Temp Values('ccaaaaa')
Insert Into #Temp Values('cca')
Insert Into #Temp Values('cccc')
Insert Into #Temp Values('ccaa')
Select * From #Temp Order by Name
This query returns bad result. Can somebody explain why?
Also, can somebody tell me which query to use to sort values.
I want to get next sequence:
1
2
3
10
aaaa
cca
ccaa
ccaaaaa
cccc
Thanks
#Shark showed you why, I'll show you a work around in your SELECT to get you the results you want:
Select * From #Temp
Order by
case isnumeric(name)
when 1 then cast(name as int)
else 999999999999999 end,
name
The actual order of results will be this:
1
10
2
3
aaaa
cca
ccaa
ccaaaaa
cccc
And SQL Server is sorting it like that because it is getting sorted based on their character values. In other words, the character 1 comes before 2, as 10 will also come before 2.
So the reason you're getting odd sorting is because your are ordering by string values, not numeric.
EDIT: Please see Adrian's answer to the workaround.