Is it possible to insert multiple values using insert in sql - sql

Is it possible to insert in one single query multiple values into a table ? .
I have declared this table
declare global temporary table CFVariables
(
CF varchar(255)
)
with replace ;
then i inserted values into the table
INSERT INTO qtemp.CFVariables ( CF ) VALUES
('F01' ), ('T01' ), ('U01' ), ('CIP' ), ('L01' )
Is it possible to not insert the values in qtemp.CFVariables table this way ? but like In ('F01' , 'T01' , 'U01' , 'CIP' , 'L01' )
Then , i declared my second table :
declare global temporary table xVariables
(
CFC numeric(3),
CF varchar(255)
)
with replace ;
In this part i'm having a problem to insert into my table xVariables
I tried to use this to insert multiple values
INSERT INTO qtemp.xVariables ( CFC, CF ) VALUES
( 1, (select CF from qtemp.CFVariables ))
My query field because i'm inserting more then one row to the table .
How can i achieve this ?

Try
INSERT INTO qtemp.xVariables ( CFC, CF ) SELECT 1 AS CFC,CF from qtemp.CFVariables;

Try running an insert-select:
INSERT INTO qtemp.xVariables ( CFC, CF )
select 1, CF from qtemp.CFVariables
To restrict the records to be inserted, you will need to do something like this:
INSERT INTO qtemp.xVariables ( CFC, CF )
select 1, CF
from qtemp.CFVariables
where CF in ('F01' , 'T01' , 'U01' , 'CIP' , 'L01' )

Related

sql procedure auto iincrement if exist

procedure [dbo].[InsertSortCode] (
#Sortcode varchar(25)
, #verbiage varchar(200) )
as
begin
SET IDENTITY_INSERT appmaster on
insert into AppMaster(MainID,SortCode) values (
(select MAX(mainid)
from AppMaster) + 1, #Sortcode )
SET IDENTITY_INSERT appmaster off
insert into Verbiage(MenueID,verbiage) values (
(select MAX(mainid)
from AppMaster), #verbiage )
the above is my stored procedure it is working perfectly fine but there is an error if there is no data in the database
the problem is with max function ,if the is no data in database
it cannot max out the id it works only if there is already some data
with its id in that databse
need to figure out how to use the exist statement on this SP so that it can work with both empty table and filled table
You can try following query:-
insert into AppMaster(MainID,SortCode) values (
(select ISNULL(MAX(mainid),0)
from AppMaster) + 1, #Sortcode )
So if MAX(mainid) is null it will return as 0 and add 1 to that.

Selection order for an Insert Into SQL Server 2008 R2

I have a query in which I create a table and then insert data into it. My question is this, I have one column where I need to make the data distinct, my output seems to be working correctly but I would like clarification on if this is an acceptable practice.
Here is what my query looks like:
DECLARE #T1 TABLE(
MRN VARCHAR(20)
, [ENCOUNTER] VARCHAR(200)
, ...
, ...
, ...
)
INSERT INTO #T1
SELECT
A.Med_Rec_No
, A.[VISIT ID]
, ...
, ...
, ...
)
FROM (
SELECT DISTINCT PAV.PTNO_NUM AS [VISIT ID] -- MUST I CHANGE THE ORDER?
, PAV.Med_Rec_No -- MUST I CHANGE THE ORDER?
, ...
, ...
, ...
)
Does the INSERT INTO #T1 SELECT take care of the ordering for me, meaning does it really matter what order I SELECT items in the FROM statement as long as my INSESRT INTO matches what my DECLARE #T1 TABLE says?
Thank you,
When doing INSERT INTO FROM SELECT you MUST match order of columns in SELECT statement as they are in INSERT statement.
Now on other hand you not required to match column order in INSERT statement to what is in CREATE TABLE.
It is always recommended to specify COLUMN in INSERT statement. Otherwise you assuming that what you selecting matches column order in table that you are inserting into.
In your case you should modify your query like so.
INSERT INTO #T1 (MRN, Encounter)
SELECT
A.Med_Rec_No
, A.[VISIT ID]
, ...
, ...
, ...
)
FROM (
SELECT DISTINCT PAV.PTNO_NUM AS [VISIT ID]
, PAV.Med_Rec_No
, ...
, ...
, ...
)
as alternative you can modify order of column in INSERT clause
INSERT INTO #T1 (Encounter,MRN)
SELECT
A.[VISIT ID]
,A.Med_Rec_No
, ...
, ...
, ...
)
FROM (
SELECT DISTINCT PAV.PTNO_NUM AS [VISIT ID]
, PAV.Med_Rec_No
, ...
, ...
, ...
)

Inserting temp table values into a table.

I have a temp table declared
declare #tmptable(
value nvarchar(500) not null
);
I use a function to insert values into that temp table.
I am trying to figure out how to update a table using the values of #tmptable
insert into t1 (
active
,SchoolId
,inserted
)
select
1
,temp.value
,#insertedDate
select temp.value from #tmptable;
When i try to insert in table t1 it doesn't work. I guess there are two Select statements is causing the problem. Please let me know how to fix it. Thanks
Try this one -
INSERT INTO dbo.t1
(
Active
, SchoolId
, Inserted
)
SELECT
1
, t.value
, #insertedDate
FROM #tmptable t;
INSERT INTO t1
(
ACTIVE
,SchoolId
,INSERTED
)
SELECT 1
,temp.value
,#insertedDate
FROM #tmptable temp;
insert into t1 (
active
,SchoolId
,inserted
)
select
1
,temp.value
,#insertedDate
from #tmptable;
this will work...

INSERT SELECT and IGNORE_DUP_KEY, how can I retrieve discarded rows?

Using MS-SQL, I have an INSERT ... SELECT statement that populates a table that has a unique key with IGNORE_DUP_KEY = ON.
Is there an easy way I could get the rows that were discarded because they were duplicated? (Preferably after the statement has completed)
The OUTPUT clause is the key here.
create table dbo.IDKsource (
SNumber int not null,
SText varchar(100) not null
) ;
go
insert into dbo.IDKsource
values ( 1, 'aaaaa' ), ( 2, 'bbbbb' ), ( 1, 'cccccc' ), ( 3, 'dddddd' ) ;
go
create table dbo.IDKOntarget (
SNumber int not null unique with ( ignore_dup_key = on ),
SText varchar(100) not null
) ;
go
-- The following lines must all be in one batch!
declare #RecordsWereInserted table (
SNumber int not null ,
SText varchar(100) not null
) ;
insert into dbo.IDKOntarget ( SNumber , SText )
output inserted.* into #RecordsWereInserted
select SNumber , SText
from dbo.IDKSource ;
select SNumber , SText
from dbo.IDKsource
except
select SNumber , SText
from #RecordsWereInserted ;
If you want to keep the inserted data for longer than one batch (or your version of SQL Server does not support table variables) then replace my table variable RecordsWereInserted with an actual table.
Note: My first approach was to use the INSERT statement directly with the EXCEPT but SQL Server will not allow DML statements with EXCEPT, INTERSECT or UNION.

SQL Server Simple Group by query

I have a simple problem , Although i believe its simple , am not able to figure out the same.
Consider i have the below table with exactly same data as given below :
CREATE TABLE #temp
(
link varchar(255),
number INT,
fname varchar(255)
)
insert into #temp VALUES ('abc',1,'f1')
insert into #temp VALUES ('abc',2,'f2')
insert into #temp VALUES ('abc',3,'f3')
insert into #temp VALUES ('abc',4,'f6')
insert into #temp VALUES ('abc',10,'f100')
insert into #temp VALUES ('abe',-1,'f0')
insert into #temp VALUES ('abe',1,'f1')
insert into #temp VALUES ('abe',2,'f2')
insert into #temp VALUES ('abe',3,'f3')
insert into #temp VALUES ('abe',4,'f6')
insert into #temp VALUES ('abe',20,'f200')
insert into #temp VALUES ('cbe',-1,'f0')
insert into #temp VALUES ('cbe',1,'f1')
insert into #temp VALUES ('cbe',2,'f2')
insert into #temp VALUES ('cbe',3,'f3')
Now for a given link , i need to get the max 'number' and the corresponding 'fname' which has the max 'number' for the given 'link'.
1)Ex : if link is 'abc' , output should be
abc, 10, f100
2)Ex : if link if 'abe' , Output should be
abe, 20, f200
3)Now link can be also given as a pattern , like (link like 'ab%') , so output should be
abc, 10, f100
abe, 20, f200
4)if (link like 'cb%') , so output should be
cbe, 3, f3
Any help in writing this group by query. I have a solution using CAST and string concat like below , but that seems to be in-efficient.
select link,number,fname from #temp
where link like 'ab%' and link+'_'+CAST(number AS varchar(255))
in (select link+'_'+CAST(MAX(number) AS varchar(255)) from #temp
group by link)
Thanks..
Using a self join:
SELECT x.link,
x.number,
x.fname
FROM #temp x
JOIN (SELECT t.link,
MAX(t.number) AS max_number
FROM #temp t
GROUP BY t.link) y ON y.link = x.link
AND y.max_number = x.number
Using a CTE and ROW_NUMBER (SQL Server 2005+):
WITH cte AS (
SELECT x.link,
x.number,
x.fname,
ROW_NUMBER() OVER(PARTITION BY x.link
ORDER BY x.number DESC) rank
FROM #temp x)
SELECT c.link,
c.number,
c.fname
FROM cte c
WHERE c.rank = 1