Showing posts with label wise. Show all posts
Showing posts with label wise. Show all posts

Monday, March 26, 2012

Row wise operation in MSSQL Server

How can I create a row wise trigger in MSSQL Server .

CREATE TRIGGER trigName ON tableName for
INSERT , UPDATE , DELETE
AS ...

For a multiple delete , I got only one trigger invocation .
But I need individual trigger calls for each row ...
How can I do this in t-sql ?
Is there any usage like FOR EACH ROW in Oracle ?
Is it possible through INSTEAD OF TRIGGER ?
Please help !!!!!!!!Yes, you could put a cursor in the trigger that operates on each row of the INSERTED table. But do yourself a favor and don't do it. Find a set-based solution using the INSERTED table instead.

Please describe what you are trying to do and why you think you need a row-wise operator (cursor). These are generally only needed by dynamic sql procedures or processes where the results of the operation on one record affects the results of the operation on the next record.|||AND...I'll add...anything that would require a cursor should not be done in a trigger...sql

Tuesday, March 20, 2012

row info wanted in column (like cube)

Hi

I have a table called tblsample, where i have information stored row wise. Ther four quarter information is stored for many years. I want those information column wise for a given year.

say

select col1, col2 from tblsample where rqtr=1 and ryear = 2000
select col1, col2 from tblsample where rqtr=2 and ryear = 2000
select col1, col2 from tblsample where rqtr=3 and ryear = 2000
select col1, col2 from tblsample where rqtr=4 and ryear = 2000

i want information like

for the Year 2000

1 qtr 2 qtr 3 qtr 4 qtr

How to acheive this in MSSQL 2000there are several ways select col1, sum(s1) as q1, sum(s2) as q2, sum(s3) as q3, sum(s4) as q4
from (
select col1, col2 as s1, 0 as s2, 0 as s3, 0 as s4
from tblsample where rqtr=1 and ryear = 2000
union all
select col1, 0, col2, 0, 0,
from tblsample where rqtr=2 and ryear = 2000
union all
select col1, 0, 0, col2, 0
from tblsample where rqtr=3 and ryear = 2000
union all
select col1, 0, 0, 0, col2
from tblsample where rqtr=4 and ryear = 2000
) dt
group by col1|||sorry forgot to add one column.

This exercise is for 50 employees

means

select empname, col1, col2 from tblsample where rqtr=1 and ryear = 2000
select empname, col1, col2 from tblsample where rqtr=2 and ryear = 2000
select empname, col1, col2 from tblsample where rqtr=3 and ryear = 2000
select empname, col1, col2 from tblsample where rqtr=4 and ryear = 2000

i need an output like
-------------------
Name 1 qtr 2 qtr 3 qtr 4 qtr
-------------------
1st emp
2nd emp
3rd emp
50th emp
--------------------|||which 50

just rework the query i gave you to add the extra column