SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sqlfresher2k7
Aged Yak Warrior

583 Posts

Posted - 06/13/2012 :  16:08:20  Show Profile  Reply with Quote

I wanted a trigger when a record new created.

In the below query results..
after the field value "WAS" inserted into the table then i want to update the "isnow" value in the table..

Please advise and help...

declare @sqlfresh table(id int identity(1,1), value varchar(max))
INSERT INTO @sqlfresh
values('2.16.840.1.114222')
INSERT INTO @sqlfresh
values('2.16.840.1.114222.65.61')

 

;with cteJunk(id, policy#, yougi, value)			
AS
(
SELECT sq.id, Tags.val.value('.', 'VARCHAR(MAX)') AS policy#,
       CASE 
          WHEN LEN(Tags.val.value('.', 'VARCHAR(MAX)')) > 4 THen  Tags.val.value('.', 'VARCHAR(MAX)')
          ELSE REPLACE(POWER(10, 5 - LEN(Tags.val.value('.', 'VARCHAR(MAX)'))  ), '1','') + Tags.val.value('.', 'VARCHAR(MAX)')
       END as yougi, value
  FROM(  
		  SELECT id, CAST('<t>' + REPLACE(value, '.', '</t><t>') + '</t>' AS XML) AS TAG  
			FROM @sqlfresh
	  ) TAB 
 CROSS APPLY TAG.nodes('/t') as Tags(val)
 inner join @sqlfresh sq
 on sq.id = TAB.id 
 ) 
 
 
SELECT
   t1.id,
   value was ,
   IsNow = substring((SELECT ( '.' + yougi )
                           FROM cteJunk t2
                           WHERE t1.id = t2.id
                           ORDER BY id
                           FOR XML PATH( '' )
                          ), 2, 1000 
                         )FROM cteJunk t1
GROUP BY id, value

visakh16
Very Important crosS Applying yaK Herder

India
47040 Posts

Posted - 06/13/2012 :  16:15:24  Show Profile  Reply with Quote
sorry question not clear. Is WAS column name or value of column? without posting DDL it would be hard to suggest solution. so please post DDL of table with sample data

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

yosiasz
Flowing Fount of Yak Knowledge

USA
1608 Posts

Posted - 06/13/2012 :  16:30:54  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
are you trying to do some sort of audit trail? i would suggest a different approach if you are trying to do some sort of audit.


<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

sqlfresher2k7
Aged Yak Warrior

583 Posts

Posted - 06/13/2012 :  17:26:45  Show Profile  Reply with Quote


Thanks for faster response..

I am not doing audit trail.. and student table structure

Id int identity(1,1), 
studentId varchar,
name varchar,
stnewid varchar


I will insert the values into the table with below query. 

insert into dbo.student
(
studentId,
name,
stnewid
) 
values
(
'2.16.840.1.114222',
'Ross',
Null
)

Insert into dbo.student
(
studentId,
name,
stnewid
) 
values
('2.16.840.1.114222.65.61',
'Karl',
Null
)

My expected output would be..



studentId                   name         stnewid 
----------------------    ----------    -----------------------------------------
2.16.840.1.114222          Ross        00002.00016.00840.00001.114222
2.16.840.1.114222.65.61    Karl        00002.00016.00840.00001.114222.00065.00061

wanted Null to be padded with zero's when inserted.
Go to Top of Page

yosiasz
Flowing Fount of Yak Knowledge

USA
1608 Posts

Posted - 06/13/2012 :  17:48:55  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
like this?


INSERT INTO dbo.student(studentId, name,stnewid)
values('2.16.840.1.114222', 'Ross', null)
INSERT INTO dbo.student(studentId, name,stnewid)
values('2.16.840.1.114222.65.61','Karl', null)

 select * from dbo.student

;with cteJunk(id, policy#, yougi, value)			
AS
(
SELECT sq.id, Tags.val.value('.', 'VARCHAR(MAX)') AS policy#,
       CASE 
          WHEN LEN(Tags.val.value('.', 'VARCHAR(MAX)')) > 4 THen  Tags.val.value('.', 'VARCHAR(MAX)')
          ELSE REPLACE(POWER(10, 5 - LEN(Tags.val.value('.', 'VARCHAR(MAX)'))  ), '1','') + Tags.val.value('.', 'VARCHAR(MAX)')
       END as yougi, studentId
  FROM(  
		  SELECT id, CAST('<t>' + REPLACE(studentId, '.', '</t><t>') + '</t>' AS XML) AS TAG  
			FROM dbo.student
	  ) TAB 
 CROSS APPLY TAG.nodes('/t') as Tags(val)
 inner join dbo.student sq
 on sq.id = TAB.id 
 ) 
 
update tgt
   SET tgt.stnewid = substring((SELECT ( '.' + yougi )
                           FROM cteJunk t2
                           WHERE tgt.id = t2.id
                           ORDER BY id
                           FOR XML PATH( '' )
                          ), 2, 1000 
                         )
  from dbo.student tgt
  --inner join cteJunk src
  --on tgt.id = src.id
  
select * From dbo.student


<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion

Edited by - yosiasz on 06/13/2012 17:54:32
Go to Top of Page

sqlfresher2k7
Aged Yak Warrior

583 Posts

Posted - 06/13/2012 :  18:48:04  Show Profile  Reply with Quote
Thanks yosiasz

How can i acheive this with trigger...
I have run the manually updates each time when insert done correct !
Go to Top of Page

yosiasz
Flowing Fount of Yak Knowledge

USA
1608 Posts

Posted - 06/13/2012 :  19:05:01  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
Something like this?


CREATE TRIGGER [dbo].[Student_it] ON [dbo].[Student]
    FOR INSERT
    AS
    BEGIN
		;with cteUpdateStudent(id, policy#, yougi, value)			
		AS
		(
		SELECT sq.id, Tags.val.value('.', 'VARCHAR(MAX)') AS policy#,
			   CASE 
				  WHEN LEN(Tags.val.value('.', 'VARCHAR(MAX)')) > 4 THen  Tags.val.value('.', 'VARCHAR(MAX)')
				  ELSE REPLACE(POWER(10, 5 - LEN(Tags.val.value('.', 'VARCHAR(MAX)'))  ), '1','') + Tags.val.value('.', 'VARCHAR(MAX)')
			   END as yougi, studentId
		  FROM(  
				  SELECT id, CAST('<t>' + REPLACE(studentId, '.', '</t><t>') + '</t>' AS XML) AS TAG  
					FROM Inserted I
			  ) TAB 
		 CROSS APPLY TAG.nodes('/t') as Tags(val)
		 inner join dbo.student sq
		 on sq.id = TAB.id 
		 ) 
		 
		update tgt
		   SET tgt.stnewid = substring((SELECT ( '.' + yougi )
								   FROM cteUpdateStudent t2
								   WHERE tgt.id = t2.id
								   ORDER BY id
								   FOR XML PATH( '' )
								  ), 2, 1000 
								 )
		  from dbo.student tgt
    END


<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

sqlfresher2k7
Aged Yak Warrior

583 Posts

Posted - 06/13/2012 :  20:21:35  Show Profile  Reply with Quote
Thanks It worked..
Go to Top of Page

yosiasz
Flowing Fount of Yak Knowledge

USA
1608 Posts

Posted - 06/13/2012 :  20:43:48  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
cool only worry I have is with the following
2.16.840.1.114222 what is the largest length value in each "octect".

<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000