Author |
Topic |
Arunavally
Yak Posting Veteran
58 Posts |
Posted - 2013-10-25 : 10:51:03
|
Hai,I used this trigger. When i insert Fees table after that this trigger will executed. But first time only execute this trigger. I dnt knw what mistake i did?This is my trigger.ALTER trigger [dbo].[Fees_FeeBalance_insertion]on [dbo].[Fees] for insertasbegindeclare @id varchar(20),@total money,@Amt money,@SFee money, @Duedate varchar(20), @fee money,@bal money,@Paydate varchar(20)select @id=Stid,@Amt=AmtPaid,@Paydate=PaidDate,@Duedate=DueDate from insertedselect @SFee=Fees from Batchif not exists (select SId from FeeBalance ) beginset @bal = @SFee-@Amtinsert into FeeBalance values(@id,@Amt,@bal,@Duedate) endelse begin select @fee=Total from FeeBalanceset @bal = @SFee - (@fee +@Amt) update FeeBalance set Total=@Amt+@fee,Balance=@bal,DueDate=@Duedate where Sid=@idendupdate StudReg set UpdationDate=@PayDate where StudReg.StuId = @idendIf any reference field is need this case?Kindly help me. |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-10-25 : 10:57:25
|
quote: Originally posted by Arunavally Hai,I used this trigger. When i insert Fees table after that this trigger will executed. But first time only execute this trigger. I dnt knw what mistake i did?This is my trigger.ALTER trigger [dbo].[Fees_FeeBalance_insertion]on [dbo].[Fees] for insertasbegindeclare @id varchar(20),@total money,@Amt money,@SFee money, @Duedate varchar(20), @fee money,@bal money,@Paydate varchar(20)select @id=Stid,@Amt=AmtPaid,@Paydate=PaidDate,@Duedate=DueDate from insertedselect @SFee=Fees from Batchif not exists (select SId from FeeBalance ) beginset @bal = @SFee-@Amtinsert into FeeBalance values(@id,@Amt,@bal,@Duedate) endelse begin select @fee=Total from FeeBalanceset @bal = @SFee - (@fee +@Amt) update FeeBalance set Total=@Amt+@fee,Balance=@bal,DueDate=@Duedate where Sid=@idendupdate StudReg set UpdationDate=@PayDate where StudReg.StuId = @idendIf any reference field is need this case?Kindly help me.
Did you mean that it affects only one row even if your insert statement inserted multiple rows?Assuming that is the case, the reason is that, the trigger is called only once for each insert statement, no matter how many rows are inserted via that statement.What that means is that the INSERTED virtual table can have more than one row. So you need to write your trigger to account for that. In your code you are getting the values from INSERTED table into scalar variables. So only one out of those many rows will be used. You should rewrite it considering INSERTED to be a table with multiple rows, and joining to that. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-25 : 22:56:57
|
[code]ALTER trigger [dbo].[Fees_FeeBalance_insertion]on [dbo].[Fees] for insertasbeginif not exists (select SId from FeeBalance ) beginset @bal = @SFee-@Amtinsert into FeeBalance select Stid,AmtPaid,(select Fees from Batch)-AmtPaid,DueDate from insertedendelse begin select @fee=Total from FeeBalanceset @bal = @SFee - (@fee +@Amt) update fbset Total=i.Amtpaid+(select Total from FeeBalance) ,Balance=(select Fees from Batch)-(i.Amtpaid + (select Total from FeeBalance)),DueDate= i.Duedate from FeeBalance fbjoin inserted ion i.Stid = fb.Stidwhere Sid=@idendupdate srset UpdationDate=i.PaidDate from StudReg srjoin inserted ion sr.StuId = i.Stidend[/code]you've not specified any relationship with Batch and FeeBalance table thats why i gave subquery assuming they've a single row. otherwise you need to add them by means of join to main table using related columns------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Arunavally
Yak Posting Veteran
58 Posts |
Posted - 2013-10-26 : 08:08:26
|
Thank you. |
|
|
Arunavally
Yak Posting Veteran
58 Posts |
Posted - 2013-10-26 : 08:11:09
|
Hai, How to split every letter of string in sql?Ex1: My string is :1234I want a=1 b=2 c=3 d=4.Ex2: My string is :124I want a=1 b=2 c=4 d=0 |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-10-26 : 08:52:02
|
Like this:declare @x varchar(26) = 1234;;with N(n) as (select 1 union all select n+1 from N where n < 26)select char(ascii('a')+n-1), SUBSTRING(CAST(@x as varchar(26)),n,1)from Nwhere n <= Len(@x); |
|
|
Arunavally
Yak Posting Veteran
58 Posts |
Posted - 2013-10-26 : 11:41:05
|
Thank you. But i want to check if a=1......if b=2.....if c=3..if d=4.....If it is possible? and how to write? Kindly help me. |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-10-26 : 12:07:37
|
[code]if (substring(cast(@x as varchar(26),n,1) = 1......if (substring(cast(@x as varchar(26),n,2) = 2.....if (substring(cast(@x as varchar(26),n,3) = 3..if (substring(cast(@x as varchar(26),n,4) = 4.....[/code] |
|
|
Arunavally
Yak Posting Veteran
58 Posts |
Posted - 2013-10-27 : 02:05:34
|
Thank you. I got error.Invalid column name n.If it need to declare n? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-27 : 02:16:27
|
quote: Originally posted by Arunavally Thank you. I got error.Invalid column name n.If it need to declare n?
Nope N is the column inside CTE.See earlier post------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Arunavally
Yak Posting Veteran
58 Posts |
Posted - 2013-10-27 : 05:06:00
|
declare @x varchar(26) = 1234;;with N(n) as (select 1 union all select n+1 from N where n < 26)select char(ascii('a')+n-1), SUBSTRING(CAST(@x as varchar(26)),n,1)from Nwhere n <= Len(@x);if (substring(cast(@x as varchar(26),n,1) = 1.......I used like this, but i got errorInvalid column name n |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-27 : 14:26:08
|
you cant use like that. Tell us whats the purpose of those if statements.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Arunavally
Yak Posting Veteran
58 Posts |
Posted - 2013-10-28 : 10:33:10
|
I solved ths issue.I want another one help.I want to select number of records inserted in the year 2013.My table has CreationDate varchar(20) field. that format is 'dd-MM-yyyy'how to select? Kindlyl help me. |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-10-28 : 11:11:02
|
One of these:SELECT COUNT(*)FROM YourTableWHERE YEAR(CAST(@dt AS DATE)) = 2013;SELECT COUNT(*)FROM YourTableWHERE @DT LIKE '%2013'; If your dateformat is not DMY, add a "SET DATEFORMAT DMY" if you want to use the first variation. |
|
|
Arunavally
Yak Posting Veteran
58 Posts |
Posted - 2013-10-28 : 11:35:09
|
Thank you. Its work well. Can i join any table SELECT COUNT(*),fb.Sum(Payables) as Exp,Sum(Receivables) as Income FROM StudReg s join Finance fb on ...... WHERE YEAR(CAST(s.CreationDate AS DATE)) = 2013;how it is work? I wnt to display Total no of students from student table and Total of Payables and Total of Receivables from finance table in the year 2013It is possible? But no link between two table |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-10-28 : 13:07:17
|
If you don't have anything to join, you cannot join them. I would do it like shown below or some variation on thatDECLARE @year INT = 2013;;WITH Students AS ( SELECT @year AS [Year],COUNT(*) AS Students FROM StudentReg WHERE YEAR(CAST(s.CreationDate AS DATE)) = @year),Finance AS ( SELECT @year AS [Year],SUM (Payables) AS Expenses,SUM(Receivables) AS Income FROM Finance WHERE YEAR(CAST(s.CreationDate AS DATE)) = @year -- or whatever isthe date column)SELECT Students, Expenses, IncomeFROM Students s INNER JOIN Finance f ON f.[Year]=s.[Year]; |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-28 : 13:07:58
|
quote: Originally posted by Arunavally Thank you. Its work well. Can i join any table SELECT COUNT(*),fb.Sum(Payables) as Exp,Sum(Receivables) as Income FROM StudReg s join Finance fb on ...... WHERE YEAR(CAST(s.CreationDate AS DATE)) = 2013;how it is work? I wnt to display Total no of students from student table and Total of Payables and Total of Receivables from finance table in the year 2013It is possible? But no link between two table
if no link it should be thisSELECT StudCnt,Exp,IncomeFROM (SELECT COUNT(*) AS StudCntFROM StudReg WHERE CreationDate > = '20130101'AND CreationDate < '20140101')sCROSS APPLY (SELECT Sum(Payables) as Exp,Sum(Receivables) as Income FROM Finance WHERE YEAR(DateField) = YEAR(s.CreationDate))f ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Arunavally
Yak Posting Veteran
58 Posts |
Posted - 2013-10-29 : 10:34:31
|
Thank you so much. I wl try |
|
|
Arunavally
Yak Posting Veteran
58 Posts |
Posted - 2013-10-29 : 11:31:46
|
Thank you. Its work wellquote: Originally posted by visakh16
quote: Originally posted by Arunavally Thank you. Its work well. Can i join any table SELECT COUNT(*),fb.Sum(Payables) as Exp,Sum(Receivables) as Income FROM StudReg s join Finance fb on ...... WHERE YEAR(CAST(s.CreationDate AS DATE)) = 2013;how it is work? I wnt to display Total no of students from student table and Total of Payables and Total of Receivables from finance table in the year 2013It is possible? But no link between two table
if no link it should be thisSELECT StudCnt,Exp,IncomeFROM (SELECT COUNT(*) AS StudCntFROM StudReg WHERE CreationDate > = '20130101'AND CreationDate < '20140101')sCROSS APPLY (SELECT Sum(Payables) as Exp,Sum(Receivables) as Income FROM Finance WHERE YEAR(DateField) = YEAR(s.CreationDate))f ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
|
|
|
Arunavally
Yak Posting Veteran
58 Posts |
Posted - 2013-10-31 : 07:59:31
|
Hai, How to get last inserted record from table without id desc option.Kindly help me. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-31 : 08:27:55
|
quote: Originally posted by Arunavally Hai, How to get last inserted record from table without id desc option.Kindly help me.
get last generated id value using SCOPE_IDENTITY and use it to retrieve last recordsie likeDECLARE @LastID intINSERT table.... your actual insert codeSET @LastID = SCOPE_IDENTITY()SELECT *FROM TableWHERE IDCol = @LastID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Next Page
|