Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-07-31 : 00:32:41
|
hi iam two tables emrtpbilleditems and emrappointmentdetails1)tp_bill_number and appointment_ids are the columns in emrtpbilleditems table.2)bill_number and appointment_id are the columns available in emrappointmentdetails tablenow i have to update the tp_bill_number values into bill_number columnbut appointmnet_ids column values are like 201,202,203 and is having relationship to column appointment_id in emrappointmentdetails table.we need to fetch and parse based on the appointment_ids values and have to update the bill_bumber column.can you help me out in thisi need output to beappointment_id bill_number201 12202 12203 12. |
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-07-31 : 00:39:31
|
can u post the sample data in the above two table ? |
|
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-07-31 : 00:49:52
|
select * from emrappointmentdetailsappointment_id patient_id bill_number8021343 805800 NULL8021372 805802 NULL8021377 805793 NULL8021378 805793 NULL8021379 805793 NULL8021381 805792 20500003338021382 805792 20500003338021383 805792 20500003338021384 805792 20500003338021385 805792 2050000333select * from emrtpbilleditems tp_id app_id appointment_ids tp_bill_number803937 805794 NULL 2010000770803938 805795 NULL 2010000770803939 805796 NULL 2010000770803940 NULL 805834,805835,805836 2070000167803941 805800 NULL 2070000168803942 NULL 805803,805804,805805,805806 2010000772803943 805811 NULL 2070000169803944 805812 NULL 2070000169803945 805813 NULL 2070000169803946 804302 NULL 2070000170803947 805819 NULL 2070000171803948 805817 NULL 2010000773803949 805802 NULL 2010000774803950 805797 NULL 2010000775803951 805798 NULL 2010000776803952 805801 NULL 2010000777803953 805799 NULL 2010000778803954 NULL 805841,805842,805843,805844,805845,805846,805847,805848,805849,805850,805851 2070000172 |
|
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-07-31 : 01:32:54
|
CREATE TABLE EMRAppointmentDetails ( APPOINTMENT_ID numeric(20, 0) NOT NULL , PATIENT_ID VARCHAR(20) NOT NULL , BILL_NUMBER VARCHAR(50) NULL) GOCREATE TABLE EMRTPBilledItems ( BILL_ITEM_ID NUMERIC(20, 0) NOT NULL, APPOINTMENT_ID NUMERIC(20, 0), APPOINTMENT_IDS VARCHAR(100), TP_BILL_NUMBER VARCHAR(50))GOINSERT INTO EMRAppointmentDetails VALUES(2363,'3145',NULL)GOINSERT INTO EMRAppointmentDetails VALUES(2404,'3145',NULL)GOINSERT INTO EMRAppointmentDetails VALUES(8021493,'805754','2050000315')GOINSERT INTO EMRAppointmentDetails VALUES(8021494,'805754','2050000315')GOINSERT INTO EMRTPBilledItems VALUES (801501,NULL,NULL,'2030000004')GOINSERT INTO EMRTPBilledItems VALUES (803954,NULL,'805841,805842,805843,805844,805845,805846,805847,805848,805849,805850,805851','2070000172')GO |
|
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-07-31 : 01:58:09
|
hi i have written fucnction like this and updating will it work as i am not sure about it.drop function GetAPPidsGOCREATE FUNCTION dbo.GetAPPids (@bill_no int,@str varchar(4000))RETURNS @appids TABLE(bill_number int, appointment_id int)BEGIN DECLARE @appid int, @delimeter int, @beginid int, @charcnt int SET @beginid=1 SET @delimeter = CHARINDEX(',', @str, @beginid) SET @appid = substring(@str,@beginid,@delimeter-1) INSERT INTO @appids values(@bill_no, cast(@appid as int)) WHILE CHARINDEX(',', @str, @beginid)>0 BEGIN SET @beginid = @delimeter+1 SET @delimeter = CHARINDEX(',', @str, @beginid) If @delimeter<>0 SET @charcnt = @delimeter-@beginid Else SET @charcnt = len(@str)-@beginid+1 SET @appid = substring(@str,@beginid,@charcnt) INSERT INTO @appids values(@bill_no, cast(@appid as int)) END RETURNENDGOselect * from dbo.GetAPPids(2010000704,'805807,805808,805809,805810')sp_help emrtpbilleditemsdeclare@bill_no int,@str varchar(4000);begin update emrappointmentdetails set bill_number=(select @bill_no from dbo.GetAPPids where @str='805807,805808,805809,805810')end |
|
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-07-31 : 04:47:07
|
hi how to write this oracle query into mssqlMERGE INTO emrappointmentdetails t1USING emrtpbilleditems t2ON (t1.appointment_id = t2.appointment_ids)WHEN MATCHED THEN UPDATE SET t1.bill_number = t2.tp_bill_numberWHEN NOT MATCHED THEN INSERT(appointment_id, patient_id, bill_number)VALUES(T2.appointment_ids, null, t2.tp_bill_number); |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-31 : 04:49:18
|
HIHere formatted query........MERGEINTO emrappointmentdetails t1USING emrtpbilleditems t2ON ( t1.appointment_id = t2.appointment_ids )WHEN MATCHED THENUPDATESET t1.bill_number = t2.tp_bill_numberWHEN NOT MATCHED THENINSERT ( appointment_id, patient_id , bill_number ) VALUES ( T2.appointment_ids, NULL , t2.tp_bill_number ); -------------------------R.. |
|
|
Sachin.Nand
2937 Posts |
Posted - 2009-07-31 : 05:16:11
|
quote: Originally posted by rajasekhar857 hi how to write this oracle query into mssqlMERGE INTO emrappointmentdetails t1USING emrtpbilleditems t2ON (t1.appointment_id = t2.appointment_ids)WHEN MATCHED THEN UPDATE SET t1.bill_number = t2.tp_bill_numberWHEN NOT MATCHED THEN INSERT(appointment_id, patient_id, bill_number)VALUES(T2.appointment_ids, null, t2.tp_bill_number);
If you are using SQL 2008 it has this new feature of Merge statement.http://technet.microsoft.com/en-us/library/bb510625.aspxPBUH |
|
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-07-31 : 05:46:11
|
no we are using sql server 2005 only is it fine Update emrappointmentdetails_bak1Set bill_number = B.tp_bill_numberfrom emrappointmentdetails_bak1 A inner join emrtpbilleditems_bak1 Bon cast(A.appointment_id as varchar) = B.appointment_idsgoif we use like this for merge statement |
|
|
Sachin.Nand
2937 Posts |
Posted - 2009-07-31 : 06:20:41
|
Yes it will work.PBUH |
|
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-07-31 : 08:12:51
|
drop function GetAPPidsGOCREATE FUNCTION dbo.GetAPPids (@bill_no int,@str varchar(4000))RETURNS @appids TABLE(bill_number int, appointment_id int)BEGINDECLARE @appid int, @delimeter int, @beginid int, @charcnt intSET @beginid=1SET @delimeter = CHARINDEX(',', @str, @beginid)SET @appid = substring(@str,@beginid,@delimeter-1)INSERT INTO @appids values(@bill_no, cast(@appid as int))WHILE CHARINDEX(',', @str, @beginid)>0BEGINSET @beginid = @delimeter+1SET @delimeter = CHARINDEX(',', @str, @beginid)If @delimeter<>0SET @charcnt = @delimeter-@beginidElseSET @charcnt = len(@str)-@beginid+1SET @appid = substring(@str,@beginid,@charcnt)INSERT INTO @appids values(@bill_no, cast(@appid as int))ENDRETURNENDGOhi how can i use an update statement in a function in a generalised way rather than passing a parameter all the time.like in a single update statement i want to update all |
|
|
Sachin.Nand
2937 Posts |
Posted - 2009-07-31 : 09:06:30
|
uhh..?I dont think it is possible atleast I dont know any of such a generalised way.PBUH |
|
|
|
|
|
|
|