I am trying to use a filter on an OUTPUT clause in t-sql.
What I want to do is something like this:
Insert into tbl_1(col1,col2)
Output Inserted.col1 into #tbl_temp
**where col1 > 0**
select col3, col4
from tbl_2
For performance reasons I don't want to use two insert statements.
insert into #tbl_temp
select col1
from
(
insert into tbl_1(col1,col2)
output Inserted.col1
select col3, col4
from tbl_2
) as T
where T.col1 > 0
Related
I am trying to use a filter on an OUTPUT clause in t-sql.
What I want to do is something like this:
Insert into tbl_1(col1,col2)
Output Inserted.col1 into #tbl_temp
**where col1 > 0**
select col3, col4
from tbl_2
For performance reasons I don't want to use two insert statements.
insert into #tbl_temp
select col1
from
(
insert into tbl_1(col1,col2)
output Inserted.col1
select col3, col4
from tbl_2
) as T
where T.col1 > 0
I am trying to get a pivot result with no aggregation, I tried max and it didn't help, may be I am doing something wrong.
When I run this below query
declare #t table
(
col1 int,
col2 varchar(100),
col3 varchar(100),
col4 varchar(100),
col5 int
)
insert into #t values(1,'test1','p1','v1',1)
insert into #t values(1,'test1','p2','v2',2)
insert into #t values(1, 'test1','p3','v3',3)
insert into #t values(1,'test1','p1','v11',1)
insert into #t values(1,'test1','p1','v12',1)
insert into #t values(1,'test1','p2','v21',2)
insert into #t values(1,'test1','p2','v22',2)
--select * from #t
select col1,
col2,
[p1],
[p2],
[p3]
from
(
select * from #t
) x
pivot
(
Max(col4 )
for col3 in ([p1],[p2],[p3])
) pvt
I get this below result
I am trying to get this below result
It would be great if you could show me a path to achieve this.
You'll still need to use an aggregate function with the PIVOT, but you need some sort of value to return multiple rows based on the combination of col1, col2, and col3. This is where you'd want to use a windowing function like row_number().
If you use the following query you should be able to get the result:
select col1, col2, p1, p2, p3
from
(
select col1, col2, col3, col4,
rn = row_number() over(partition by col1, col2, col3 order by col5, col4)
from #t
) d
pivot
(
max(col4)
for col3 in (p1, p2, p3)
) p;
See SQL Fiddle with Demo
The row_number() function creates a unique sequence that is partitioned by the col1, col2 and col3 values - I then ordered the results by your col5 and col4 values to create the sequence in a specific order. This new value is used when the pivot groups the data which results in multiple rows being returned instead of the single row.
I want to INSERT INTO by more than one value at a time, I did this by doing the following:
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, (SELECT col3 FROM table_3)
FROM table_2
But col3 from table_3 is a datetime format, while col3 from table_1 needs an integer value. I did this by doing the following:
CAST(CONVERT(varchar(10),(SELECT col3 FROM table_3),112)AS int)
When I run this I get a more than one result in a subquery error. I have really no idea whatsoever on how to fix this. Hopefully you do.
Thank you in advance.
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, (CAST(CONVERT(varchar(10),(SELECT top 1 col3 FROM table_3),112)AS int))
FROM table_2
You need to use top 1 to select 1 row
Well, I think the error says it all. You have to limit the inner query somehow with WHERE condition, with TOP or with MAX(col3) for example. Depends WHICH col3 you want.
You need to join table_2 to table_3. Not sure what your database structure is, but it should be something like this:
INSERT INTO table_1(col_1,col_2,col_3)
SELECT t2.col_1, t2.col2, t3.col3
FROM table_2 t2
INNER JOIN table_3 t3 on t3.t2id = t2.id
The alternative is to use TOP 1, to just return 1 record in the sub-query - but I would not recommend this as it may not be the value you want:
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, (SELECT top 1 col3 FROM table_3)
FROM table_2
You can use CTE to prepare your data :
;WITH MyData (col1, col2, col3)
AS
(
SELECT col_1, col2, CAST(CONVERT(varchar(10),(col3),112)AS int)
FROM table_2 JOIN table_3 ON <join condition>
)
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, col3
FROM MyData
Pls try below query :
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, isnull((SELECT TOP 1 cast(col3 as int) FROM table_3),0)
FROM table_2
I need to make a trigger that, upon an insert into table1, checks if the same values from 3 columns in table2 match, if not it inserts that row into table2. If there is a match it then updates the row that matches. Here's what I've gathered so far, but it doesn't do the IF EXISTS check I need it to. I'm not certain how exactly to structure it in this case due to never having worked with triggers.
CREATE TRIGGER Trigger_Name on Table_Name
FOR INSERT
AS
BEGIN
INSERT INTO
TABLE 2
(
Col1,
Col2,
Col3,
Col4
)
SELECT
(
Col1,
Col2,
Col3,
Col4
)
FROM
INSERTED
GO
The IF EXISTS criteria need to see if table1.col1=table2.col1, table1.col2=table2.col2, table1.col3,table2.col3
Using SQL server 2008. Any help is much appreciated
UPDATE table2
SET col1 = inserted.col1
, col2 = inserted.col2
, col3 = inserted.col3
, col4 = inserted.col4
FROM table2
INNER
JOIN inserted
ON inserted.col1 = table2.col1
AND inserted.col2 = table2.col2
AND inserted.col3 = table2.col3
;
INSERT INTO table2 (col1, col2, col3, col4)
SELECT col1
, col2
, col3
, col4
FROM inserted
WHERE NOT EXISTS (
SELECT *
FROM table1
WHERE col1 = inserted.col1
AND col2 = inserted.col2
AND col3 = inserted.col3
)
;
I want to do something like
insert into my table (select * from anothertable where id < 5)
What is the correct MSSQL syntax?
Thanks!
Is this what you're looking for?
INSERT INTO MyTable
SELECT * FROM AnotherTable
WHERE AnotherTable.ID < 5
That syntax looks correct, but you the fields have to match exactly otherwise it won't work.
You can specify the fields eg:
INSERT INTO myTable(COL1, COL2, COL3)
SELECT COL1, COL2, COL3 FROM anotherTable where anotherTable.id < 5
Insert Into MyTable
(
Col1,
Col2,
Col3
)
Select
Col1,
Col2,
Col3
From
AnotherTable
Where
ID < 5
You can also do
select *
into MyTable
from AnotherTable
where ID < 5
which will create MyTable with the required columns, as well as fill the data in.