| Author |
Topic |
|
adam
Starting Member
4 Posts |
Posted - 2008-02-03 : 08:59:35
|
| Hi All,I have two tables.First is CAT_TABLE_LIST ;FIELD_NAME OPPOSITE_NAME TBL_NAMEHeight (mm) , COL_1 , TBL_13Cylinder volume (cm³) , COL_5 , TBL_13Torgue (daN.m) , COL_7 , TBL_13Cylinder weight(kg) , COL_13 , TBL_13Pump Type , COL_9 , TBL_13Fuel Consumption , Col_22 , TBL_22Color , Col_41 , TBL_09--------------------------------------------------------------------Second is TBL_13 (there are a few tables for each distict value for CAT_TABLE_LIST.TBL_NAME column );STOCK_CODE COL_1 COL_5 COL_7 COL_13 COL_9FC-25 , 12,5 , 5 , 15.000 , 45 , DP 75FT-45 , 27 , 9 , 40.000 , 35 , DP 101--------------------------------------------------------------------the CAT_TABLE_LIST.OPPOSITE_NAME column holds columns names of tables(for TBL_NAME).How can i get a result with specific TBL_NAME and STOCK_CODE ?expected result is (for TBL_NAME = 'TBL_13' AND STOCK_CODE ='FC-25' ) .....STOCK_CODE OPPOSITE_NAME FIELD_NAME VALUEFC-25 , COL_1 , Height (mm) , 12,5FC-25 , COL_5 , Cylinder volume (cm³), 5FC-25 , COL_7 , Torgue (daN.m) , 15.000FC-25 , COL_13 , Cylinder weight(kg) , 45FC-25 , COL_9 , Pump Type , DP 75----------------------------------------------------------Thanks in advance...PS: i use ,(comma) for separator in data rows.. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-03 : 10:22:45
|
You need to use dynamic sql for achieveing as your your second tables info itself is coming from a column of first table.CREATE FUNCTION GetValues( @StockCode varchar(5) )RETURNS @Results TABLE(STOCK_CODE varchar(5),OPPOSITE_NAME varchar(5),FIELD_NAME varchar(15),VALUE int)ASBEGINDECLARE @PK int,@JoinTable varchar(10),@ReqdCol varchar(10),@Sql varchar(8000)SELECT @PK = MIN(PKCol)FROM CAT_TABLE_LIST WHILE @PK IS NOT NULLBEGINSELECT @JoinTable =TBL_NAME, @ReqdCol=OPPOSITE_NAME FROM CAT_TABLE_LIST WHERE PKCol=@PKSELECT @Sql='SELECT j.STOCK_CODE,c.OPPOSITE_NAME,c.FIELD_NAME,j.' + @ReqdCol + ' FROM CAT_TABLE_LIST c CROSS JOIN ' + @JoinTable +' j WHERE c.OPPOSITE_NAME =' + @ReqdCol + ' AND c.TBL_NAME=' + @JoinTable + ' AND j.STOCK_CODE=' + @StockCode INSERT INTO @ResultsEXEC (@Sql)SELECT @PK = MIN(PKCol)FROM CAT_TABLE_LIST WHERE PKCol > @PKENDENDGO invoke the function like thisSELECT * FROM GetValues('FC-25')also note that PKCol is the primary key column of CAT_TABLE_LIST table |
 |
|
|
adam
Starting Member
4 Posts |
Posted - 2008-02-03 : 10:59:47
|
| hi visakh16,I am getting these error.Msg 443, Level 16, State 14, Procedure GetValues, Line 28Invalid use of side-effecting or time-dependent operator in 'INSERT EXEC' within a function.Msg 455, Level 16, State 2, Procedure GetValues, Line 31The last statement included within a function must be a return statement. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-04 : 03:23:11
|
make it a procedureCREATE TABLE CAT_TABLE_LIST (FIELD_NAME varchar(50),OPPOSITE_NAME varchar(20),TBL_NAME varchar(20))INSERT INTO CAT_TABLE_LIST VALUES ('Height (mm)' , 'COL_1' , 'TBL_13')INSERT INTO CAT_TABLE_LIST VALUES ('Cylinder volume (cm³)' , 'COL_5' , 'TBL_13')INSERT INTO CAT_TABLE_LIST VALUES ('Torgue (daN.m)' , 'COL_7' , 'TBL_13')INSERT INTO CAT_TABLE_LIST VALUES ('Cylinder weight(kg)' , 'COL_13' , 'TBL_13')INSERT INTO CAT_TABLE_LIST VALUES ('Pump Type' , 'COL_9' , 'TBL_13')INSERT INTO CAT_TABLE_LIST VALUES ('Fuel Consumption' , 'Col_22' , 'TBL_22')INSERT INTO CAT_TABLE_LIST VALUES ('Color' , 'Col_41' , 'TBL_09')select * from TBL_13CREATE TABLE TBL_13(STOCK_CODE varchar(10),COL_1 decimal(10,1),COL_5 int,COL_7 decimal(10,3),COL_13 int,COL_9 varchar(10))INSERT INTO TBL_13 VALUES ('FC-25' , 12.5 , 5 , 15.000 , 45 , 'DP 75')INSERT INTO TBL_13 VALUES ('FT-45' , 27 , 9 , 40.000 , 35 , 'DP 101')CREATE TABLE TBL_09(STOCK_CODE varchar(10),COL_41 decimal(10,1),COL_5 int,COL_7 decimal(10,3),COL_13 int,COL_9 varchar(10))INSERT INTO TBL_09 VALUES ('KC-27' , 10.23 , 5 , 15.000 , 33 , 'KR 168')CREATE TABLE TBL_22(STOCK_CODE varchar(10),COL_11 decimal(10,1),COL_15 int,COL_22 decimal(10,3),COL_23 int,COL_19 varchar(10))INSERT INTO TBL_22 VALUES ('KM-29' , 12.5 , 5 , 15.000 , 45 , 'DP 75')INSERT INTO TBL_22 VALUES ('FC-25' , 123 , 2 , 40.250 , 35 , 'KK 324')DROP table ResultsCREATE TABLE Results --table to get results(STOCK_CODE varchar(10),OPPOSITE_NAME varchar(20),FIELD_NAME varchar(50),[VALUE] varchar(50))CREATE PROCEDURE GetValues @StockCode varchar(5)ASBEGINDECLARE @PK varchar(50),@JoinTable varchar(10),@ReqdCol varchar(10),@Sql varchar(8000)SELECT @PK=MAX(FIELD_NAME)FROM CAT_TABLE_LIST WHILE @PK IS NOT NULLBEGINSELECT @JoinTable =TBL_NAME, @ReqdCol=OPPOSITE_NAME FROM CAT_TABLE_LIST WHERE FIELD_NAME=@PK SET @Sql='INSERT INTO ResultsSELECT j.STOCK_CODE,c.OPPOSITE_NAME,c.FIELD_NAME ,j.' + @ReqdCol + ' FROM CAT_TABLE_LIST c CROSS JOIN ' + @JoinTable +' j WHERE c.OPPOSITE_NAME =''' + @ReqdCol + ''' AND c.TBL_NAME=''' + @JoinTable + ''' AND j.STOCK_CODE=''' + @StockCode +'''' EXEC (@Sql)SELECT @PK = MAX(FIELD_NAME)FROM CAT_TABLE_LIST WHERE FIELD_NAME < @PKENDRETURNENDGOGetValues 'FC-25'select * from ResultsOutput-----------------STOCK_CODE OPPOSITE_NAME FIELD_NAME VALUEFC-25 COL_7 Torgue (daN.m) 15.000FC-25 COL_9 Pump Type DP 75FC-25 COL_1 Height (mm) 12.5FC-25 Col_22 Fuel Consumption 40.250FC-25 COL_13 Cylinder weight(kg) 45FC-25 COL_5 Cylinder volume (cm³) 5 |
 |
|
|
adam
Starting Member
4 Posts |
Posted - 2008-02-04 : 08:31:14
|
| Hi visakh16,Thanks for your sample code.is it possible to format values of j.' + @ReqdCol + ' column. When inserting to Results table ?I mean if it is decimal, discarding unnecessary zero after the decimal separator will be great. And exactly the same for DateTime values.if j.' + @ReqdCol + ' colums value decimal lie;23.2000 --> 23.223.0100 --> 23.0123.000 -->23and for datetime, small date time will be enough like '27/01/2009'What do u think ?thanks again ... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-04 : 11:27:58
|
| try using ROUND() function |
 |
|
|
|
|
|