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-12-01 : 05:24:41
|
| hi iam having two tables A and BCOLUMNS A are b,c,COLUMNS B are a,b,cb and c are available in both tbales.now i need to insert what ever the record are there in A into Corresponding B column tables.and column a from B table should start with value 1501 and so on.how to give a stored procedure in doing so. |
|
|
vikky
Yak Posting Veteran
54 Posts |
Posted - 2009-12-01 : 05:52:43
|
| hi,CREATE PROC PROCEDURENAME ( @start_val INT = NULL)AS BEGIN SET NOCOUNT OFF SELECT @start_val = 1501 WHERE @start_val IS NULL DECLARE @a TABLE ( b INT, c INT) INSERT INTO @a SELECT 1,4 union all SELECT 3,5 union all SELECT 7,6 union all SELECT 9,7 union all SELECT 1,6 DECLARE @b TABLE ( a INT, b INT, c INT) INSERT INTO @b(a,b,c) SELECT DISTINCT @start_val+number,b,c FROM ( SELECT ROW_NUMBER()OVER (ORDER BY b,c)AS row,b,c FROM @a) AS a INNER JOIN master..spt_values AS n ON n.type = 'p' and a.row = n.number+1 SELECT * FROM @bEND SET NOCOUNT ON -------------------Execution :Exec PROCEDURENAME null ( or ) any value u need.Thanks,vikky. |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-12-01 : 05:58:35
|
| If you use 2005 or greater then....insert into bselect 1500+row_number() over (order by b,c),b,c from aSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-12-01 : 06:31:59
|
| i have written like this but values are not inserting in the another table.please checkif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Migration_PATIENTDETAILS]') AND type = 'P')drop Procedure [dbo].[Migration_PATIENTDETAILS]GOCREATE PROCEDURE [dbo].[Migration_PATIENTDETAILS](@start_val INT = NULL)ASBEGINSET NOCOUNT OFFSELECT @start_val = 1501 WHERE @start_val IS NULLDECLARE @EMRFEMALEPATIENTDETAILS TABLE ( PATIENT_ID INT, ENCOUNTER_ID INT)INSERT INTO @EMRFEMALEPATIENTDETAILSSELECT 1,4 union allSELECT 3,5 union allSELECT 7,6 union allSELECT 9,7 union allSELECT 1,6DECLARE @EMRFRExamData TABLE ( FR_EXAM_DATA_ID INT, PATIENT_ID INT, ENCOUNTER_ID INT)INSERT INTO @EMRFRExamData (FR_EXAM_DATA_ID,PATIENT_ID,ENCOUNTER_ID)SELECT DISTINCT @start_val+number,PATIENT_ID,ENCOUNTER_IDFROM ( SELECT ROW_NUMBER()OVER (ORDER BY PATIENT_ID,ENCOUNTER_ID)AS row, PATIENT_ID,ENCOUNTER_ID FROM @EMRFEMALEPATIENTDETAILS) AS EMRFEMALEPATIENTDETAILSINNER JOIN master..spt_values AS n ON n.type = 'p' and EMRFEMALEPATIENTDETAILS.row = n.number+1SELECT * FROM @EMRFRExamDataENDGOSET NOCOUNT ON |
 |
|
|
|
|
|
|
|