insert data to temporary table in sql 2008 - sql

CREATE TABLE #tmpt
(
ID INT
,sName varchar(20)
)
INSERT INTO #tmpt VALUES (1,'ran')
INSERT INTO #tmpt VALUES (2,'pan')
INSERT INTO #tmpt VALUES (3,'fan')
INSERT INTO #tmpt VALUES (4,'gan')
This type of insert does not work. Can you help

Related

INSERT table alias not recognised in the OUTPUT clause

Why is the INSERT table alias not recognised in the OUTPUT/INSERTED line?
EDIT: The linking table needs to be populated with new #Data_Table.Id (INSERTED.id, works), and the #NewData_Table.ObjectId (errors). SO that a 'linking table', with a foreign key relationship from #Data_Table to #Tmp_Link_Table can be created.
EDIT:
--Expected Output
--ObjectId DataId
--11 3
--12 4
--13 5
--14 6
DECLARE #NewData_Table TABLE
( [Data] VARCHAR(50) NOT NULL,
ObjectId INT NOT NULL)
DECLARE #Tmp_Link_Table TABLE
( ObjectId INT NOT NULL,
DataId INT NOT NULL)
DECLARE #Data_Table TABLE
( Id INT NOT NULL Identity(1,1),
Data VARCHAR(50) NOT NULL)
-- create new objects
INSERT INTO #NewData_Table (ObjectId, Data) VALUES (11,'Data 1')
INSERT INTO #NewData_Table (ObjectId, Data) VALUES (12,'Data 2')
INSERT INTO #NewData_Table (ObjectId, Data) VALUES (13,'Data 3')
INSERT INTO #NewData_Table (ObjectId, Data) VALUES (14,'Data 4')
SELECT * FROM #NewData_Table
-- create some data
INSERT INTO #Data_Table (Data) VALUES ('Data One')
INSERT INTO #Data_Table (Data) VALUES ('Data Two')
--#Data_Table BEFORE
SELECT * FROM #Data_Table
--Q!: Why "Msg 4104, Level 16, State 1, Line 27 The multi-part identifier "d.ObjectId" could not be bound."?
INSERT INTO #Data_Table (Data) OUTPUT d.ObjectId, INSERTED.Id INTO #Tmp_Link_Table (ObjectId, DataId)
SELECT d.Data
FROM #NewData_Table AS d
--#Data_Table AFTER
SELECT * FROM #Data_Table
--Linked table from INSERT
SELECT * FROM #Tmp_Link_Table
REF: OUTPUT Clause (Transact-SQL)
Unfortunately, with INSERT statements, you cannot output a column that is not one of the inserted columns (though I can't seem to find a reference in the documentation that explains this).
There is a way to get around this, by using a merge statement.
For example, change this part:
INSERT INTO #Data_Table (Data) OUTPUT d.ObjectId, INSERTED.Id INTO #Tmp_Link_Table (ObjectId, DataId)
SELECT d.Data
FROM #NewData_Table AS d
To this:
MERGE #Data_Table AS DT
USING #NewData_Table AS NDT
ON 1 = 2 -- This never matches...
WHEN NOT MATCHED THEN -- So this is always true...
INSERT (Data)
VALUES (NDT.Data)
OUTPUT NDT.ObjectId, INSERTED.id
INTO #Tmp_Link_Table (ObjectId, DataId);
You have to update your table schema and query as below to work:
DECLARE #NewData_Table TABLE
( [Data] VARCHAR(50) NOT NULL,
ObjectId INT NOT NULL
)
DECLARE #Tmp_Link_Table TABLE
( ObjectId INT NOT NULL,
DataId INT NOT NULL
)
DECLARE #Data_Table TABLE
( Id INT NOT NULL Identity(1,1),
Data INT
)
-- create new objects
INSERT INTO #NewData_Table (ObjectId, Data) VALUES (11,66)
INSERT INTO #NewData_Table (ObjectId, Data) VALUES (12,88)
INSERT INTO #NewData_Table (ObjectId, Data) VALUES (13,99)
INSERT INTO #NewData_Table (ObjectId, Data) VALUES (14,44)
SELECT * FROM #NewData_Table
-- create some data
INSERT INTO #Data_Table (Data) VALUES (44)
INSERT INTO #Data_Table (Data) VALUES (55)
--#Data_Table BEFORE
SELECT * FROM #Data_Table
INSERT INTO #Data_Table (Data) OUTPUT INSERTED.Data, INSERTED.Id INTO #Tmp_Link_Table (ObjectId, DataId)
SELECT d.Data
FROM #NewData_Table AS d
--#Data_Table AFTER
SELECT * FROM #Data_Table
--Linked table from INSERT
SELECT * FROM #Tmp_Link_Table
Output (#Tmp_Link_Table):

How to insert keyvalue as a row in table?

I have table variable which contains records in terms of key,value pair of a table. where key is the column of table and value is the value for that column
below is table variable
DECLARE #Table TABLE(
FieldName varchar(100),
FieldValue varchar(max))
insert into #Table values('Title','NewFrom')
insert into #Table values('Points','4')
insert into #Table values('createdby','5')
I have above values in UsersInformation.This is physical table of sql server
has following column.
UserID int autoincrement(identity)
Title nvarchar(100),
Points int,
createdby int
I want that all values of #Table should be as a single row of UserInformation table.
How can be using sql server?
Have not tested but this should work
INSERT INTO UsersInformation (Title, Points, createdby)
SELECT Title, Points, createdby FROM (SELECT FieldName, FieldValue
FROM #Table) AS SourceTable
PIVOT (MAX(FieldValue) FOR FieldName IN ([Points], [Title], [createdby])) AS PivotTable;
Based on your comment you will need to add a key to your source #table
DECLARE #Table TABLE(
id int,
FieldName varchar(100),
FieldValue varchar(max))
insert into #Table values(1,'Title','NewFrom')
insert into #Table values(1,'Points','4')
insert into #Table values(1,'createdby','5')
insert into #Table values(2,'Title','NewFrom2')
insert into #Table values(2,'Points','44')
insert into #Table values(2,'createdby','55')
insert into #Table values(3,'Title','NewFrom3')
insert into #Table values(3,'Points','444')
Then you can run the query:
SELECT [Title], [Points], [createdby]
FROM #Table
PIVOT
(
max(FieldValue) FOR [FieldName] IN ([Title], [Points], [createdby])
) AS P

Single SQLstatement for many INSERTs

Please see the DDL below:
CREATE TABLE #Test (ID INT NOT NULL IDENTITY, Name varchar(100), [Key] int, primary key (ID))
INSERT INTO #Test (Name, [Key]) values ('Ian',1)
INSERT INTO #Test (Name, [Key]) values ('Iain',1)
INSERT INTO #Test (Name, [Key]) values ('Eon',1)
INSERT INTO #Test (Name, [Key]) values ('Mark',2)
INSERT INTO #Test (Name, [Key]) values ('Steven',2)
and the DDL below:
CREATE TABLE #Test2 (ID INT NOT NULL IDENTITY, Name varchar(100), primary key (ID))
INSERT INTO #Test2 (Name) values ('Graham')
INSERT INTO #Test2 (Name) values ('William')
INSERT INTO #Test2 (Name) values ('Neil')
INSERT INTO #Test2 (Name) values ('Calum')
INSERT INTO #Test2 (Name) values ('Wayne')
I want to INSERT everything from #Test2 into #Test. I want each record in #Test2 to have a unique Key in #Test. In affect I want this to happen:
INSERT INTO #Test (Name, [Key]) VALUES ('Graham', 3)
INSERT INTO #Test (Name, [Key]) VALUES ('William', 4)
INSERT INTO #Test (Name, [Key]) VALUES ('Calum', 5)
INSERT INTO #Test (Name, [Key]) VALUES ('Wayne', 6)
There are millions of records in #Test and #Test2.
Is it possible to do all the INSERTS with a single SQL statement or do I have to use a cursor.
You need just new numbers that are higher than the max. key that exists? This should work:
declare #maxkey int
select #maxkey = max([Key]) from Test
insert into Test (Name, [Key])
select Name, row_number () over (order by (select null)) + #maxkey
from Test2
This just fetches the max. key and uses row_number to add numbers to the rows coming from Test2.
SQL Fiddle
You can try some logic like this:
insert into #Test
select Name, ((select max([Key]) from #Test) + ID) as [Key]
from #Test2
order by ID asc;
Here is my Fiddle example: Fiddle

Inserting to TYPE Table is so slow

I have a USER DEFINE TABLE TYPE that stores more than 100K rows, now when I try to insert 100k records the insertion of data is very slow. It takes me 1 minute before the insertion is finish. Any idea how to make insertion fast?
--USER DEFINE TABLE TYPE
CREATE TYPE [dbo].[BigIntegerList] AS TABLE(
[ID] [bigint] NULL
)
GO
--sample query
DECLARE
#Data dbo.BigIntegerList
insert into #Data values(1824953)
insert into #Data values(1824954)
insert into #Data values(1824955)
insert into #Data values(1824956)
insert into #Data values(1824996)
insert into #Data values(1824997)
insert into #Data values(1824998)
insert into #Data values(1824999)
insert into #Data values(1825000)
insert into #Data values(1825001)
insert into #Data values(1825002)
insert into #Data values(1825003)
insert into #Data values(1825004)
insert into #Data values(1825005)
insert into #Data values(1825006)
insert into #Data values(1825007)
select * from #Data
Why not tag this .NET?
For insert speed use TVP
maximizing-performance-with-table-valued-parameters
Even if not TVP at least pass multiple values
values (1824953), (1824954)
About 800 at a time
And does it really need to be bigint?

Insert error in sql cursor

I have this SQL Server query with a cursor:
DECLARE #ids TABLE(id varchar(50))
INSERT INTO #ids VALUES ('1098264', '1098859', '1098860', '1098267', '1098265')
But when I run the code, I get an error:
Insert Error: Column name or number of supplied values does not match table definition.
You have only one column in table i.e id and you are trying to insert 5 column values in table.
Try this
Create table :
DECLARE #ids TABLE(id varchar(50))
Insert values to table :
INSERT INTO #ids VALUES ('1098264')
INSERT INTO #ids VALUES ('1098859')
INSERT INTO #ids VALUES ('1098860')
INSERT INTO #ids VALUES ('1098267')
INSERT INTO #ids VALUES ('1098265')
You need to use a pair of bracket for each row like below:
INSERT INTO #ids VALUES ('1098264'),('1098859'),('1098860'),('1098267'),('1098265')
There are three ways of inserting multiple rows into a table (other than via a select statement)
INSERT INTO FOO (columna,columnb)
VALUES (1,a)
INSERT INTO FOO (columna,columnb)
VALUES (2,b)
INSERT INTO FOO (columna,columnb)
VALUES (3,c)
The second
INSERT INTO FOO (columna,columnb)
select 1,'a'
UNION ALL
select 2,'b'
UNION ALL
select 3,'c'
Thirdly (only works SQLServer 2008 and up)
INSERT INTO FOO (columna,columnb)
VALUES (1,'a'),(2,'b'),(3,'c')
In the example in your question, you were trying to insert multiple fields into one column, which is why you got the error.
You have only 1 column in the table but are trying to insert 5 values.
First declare 5 different columns in the table to hold 5 values
For Example:-
declare #IDS_1 varchar(50)
,#IDS_2 varchar(50)
,#IDS_3 varchar(50)
,#IDS_4 varchar(50)
,#IDS_5 varchar(50)
INSERT INTO #IDS VALUES ('1098264'
,'1098859'
,'1098860'
,'1098267'
,'1098265')