C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » SQL Server »

trigger problem.


Posted Date: 09 Jul 2006      Posted By: carrot      Member Level: Bronze     Points: 2   Responses: 3



I have 2 tables as below:

Table1: Itemlists
Fields: ItemName, ItemCode

ex:

ItemName ItemCode
-------------- -------------
Product X X
Product Y Y

Table2: Product X
Fields: autono, ProdID, Condition, ExpiredDate, OtherInfor

I wan to have the data for Product X as below:

Autono ProdID Condiiton ExpiredDate OtherInfor
---------- --------- ------------- ---------------- ----------------
1 X00001 Good 02/02/2007 xxx
2 X00002 Good 12/02/2007 yyy


I created a triigger as below:
----------------------------------------
ALTER TRIGGER Trigger_itemidinsert
ON dbo.ProductX
FOR INSERT
AS
Declare @autono int, @itemcode nvarchar(5)

Select @autono = (Select autono from Inserted)
Select @itemCode = (Select itemCode from itemlists where itemName ='Product X')

Update Product X Set ProdID = @itemcode + '@autono' where autono = @autono

this is the just a try to get the ProdID as "X1" i suppose
However, i receive the error msg as below:

Error msg: conversion failed when converting the nvarchar value 'X' to data type int.

I knew it's is convertion problem but anyone can give me advise.
I would appreciate if anyone can provide me example code to get this : prodID : X00001 instead of X1







Responses

Author: Gangarani Rajendran    11 Jul 2006Member Level: SilverRating: 2 out of 52 out of 5     Points: 2

Hi,

You have to use Cast(@autoint as varchar)

To get prodid like X00001, you have to use the string functions,

Select @autono = (Select autono from Inserted)
Select @itemCode = (Select itemCode from itemlists where itemName ='Product X')

Update Product X Set ProdID = @itemcode + Right('00000' + Cast(@autono as Varchar), 5 ) where autono = @autono


Regards,
Gangarani Rajendran



Author: Tejinder Singh Barnala    24 Dec 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 0

Gangarani is right

Many Thanks
Tejinder Singh Barnala
/*I have the simplest tastes. I am always satisfied with the best*/



Author: Roshan R Mhatre    15 Jan 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 4

there is two database with name DB11 and DB22

DB11 Contain table with Name table1 with column empid,empno,name,Salary

DB22 Contain table with Name table2 with column empid,empno,name,Salary




use DB11
Select empid,empno,name,Salary from table1

select empid,empno,name,Salary from DB22..table2 tab2

Select table1.empid,table1.name,table1.salary
from table1,DB22..table2 tab2
where tab2.empid = table1.empid order by table1.name



Hope this will Help U alot


Best Of Luck!



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Function is not working
Previous : DATE SEARCH IN ASPX PAGE
Return to Discussion Forum
Post New Message
Category: SQL Server

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use