Author |
Topic |
casati74
Posting Yak Master
109 Posts |
Posted - 2006-09-06 : 06:19:08
|
hello i wrote this query but when i lunch it don't run how???? set @sql ='SELECT DISTINCT '''+@HeatNumber+'''=Heats.HeatNumber, HeatOrders.OrderNumber, Heats.[Date] FROM HeatEditedChemistry INNER JOIN Replications ON HeatEditedChemistry.HeatEditedChemistryID = Replications.IdRegister INNER JOIN Heats ON HeatEditedChemistry.HeatID = Heats.HeatID LEFT OUTER JOIN HeatOrders ON Heats.HeatID = HeatOrders.HeatID WHERE (eVisionDonasid..Replications.IdRegister =1571)'Where is the error???Thank's |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-06 : 06:20:13
|
What IS the error?Maybe you need to convert @HeatNumber to a VARCHAR when concatenating strings?set @sql ='SELECT DISTINCT Heats.HeatNumber AS '' + @HeatNumber + ''Peter LarssonHelsingborg, Sweden |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-09-06 : 06:23:24
|
Obviously it won't run because you are trying to assign value to a variable in Dynamic SQL. Why don't you do it directly. What is the purpose of using D-SQL?SELECT DISTINCT @HeatNumber = Heats.HeatNumber, HeatOrders.OrderNumber, Heats.[Date]FROM HeatEditedChemistry INNER JOIN Replications ON HeatEditedChemistry.HeatEditedChemistryID = Replications.IdRegister INNER JOIN Heats ON HeatEditedChemistry.HeatID = Heats.HeatID LEFT OUTER JOIN HeatOrders ON Heats.HeatID = HeatOrders.HeatID WHERE (eVisionDonasid..Replications.IdRegister =1571) Harsh AthalyeIndia."Nothing is Impossible" |
|
|
casati74
Posting Yak Master
109 Posts |
Posted - 2006-09-06 : 06:28:03
|
When I exec (@sql) he don't return any value, but if i writeset @sql ='SELECT DISTINCT ''+@HeatNumber+''=Heats.HeatNumberHe return a select result witout assign value to a variable @HeatNumber.esexec @sql 1250 null 31/08/2006print @HeatNumberor select * from tabella where Heatnumber = @HeatNumberHe don't return anythingWhy |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-09-06 : 06:31:05
|
quote: He return a select result witout assign value to a variable @HeatNumber.
Have a look in bookonline for sp_Executesql Chirag |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-06 : 06:35:06
|
DON'T USE DYNAMIC SQL FOR THIS!And you can't mix selecting values into variables together with viewing data.This is how your query look likeSELECT @HeatNumber = Heats.HeatNumber, HeatOrders.OrderNumber, Heats.[Date]FROM HeatEditedChemistryINNER JOIN Replications ON HeatEditedChemistry.HeatEditedChemistryID = Replications.IdRegisterINNER JOIN Heats ON HeatEditedChemistry.HeatID = Heats.HeatIDLEFT JOIN HeatOrders ON Heats.HeatID = HeatOrders.HeatIDWHERE eVisionDonasid..Replications.IdRegister = 1571 You return three values (columns) but only store one of them?What is you objective? Haven't you learned yet to explain1. What you want to be done2. What you have tried this far3. What is the source of the data (sample value and table definitions)4. What is the expected output from the supplied sample dataPeter LarssonHelsingborg, Sweden |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-09-06 : 06:35:39
|
quote: Originally posted by casati74 When I exec (@sql) he don't return any value, but if i writeset @sql ='SELECT DISTINCT ''+@HeatNumber+''=Heats.HeatNumberHe return a select result witout assign value to a variable @HeatNumber.esexec @sql 1250 null 31/08/2006print @HeatNumberor select * from tabella where Heatnumber = @HeatNumberHe don't return anythingWhy
I don't know how you are able to run your EXEC statement without any error, if you are running the query you posted, since at the same time you are assigning value of one field to a variable and also, displaying some other fields (HeatOrders.OrderNumber, Heats.[Date]) which is simply not allowed?Please explain your scenario clearly!!Harsh AthalyeIndia."Nothing is Impossible" |
|
|
casati74
Posting Yak Master
109 Posts |
Posted - 2006-09-06 : 08:37:18
|
i have this selectset @sql ='SELECT DISTINCT Heats.HeatNumber, HeatOrders.OrderNumber, Heats.[Date]FROM '+@InvolvedTable+' INNER JOIN Replications ON '+@InvolvedTable+'.HeatEditedChemistryID = Replications.IdRegister INNER JOIN Heats ON '+@InvolvedTable+'.HeatID = Heats.HeatID LEFT OUTER JOIN HeatOrders ON Heats.HeatID = HeatOrders.HeatID WHERE (eVisionDonasid..Replications.IdRegister ='''+@IdRegister+''')'and i want assign a result to a thre variable @HeatNumber,@OrderNumber, @date because I use this data in other select.How can i do it?!?!?thank's |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-06 : 08:43:20
|
http://www.sommarskog.se/dynamic_sql.htmlPeter LarssonHelsingborg, Sweden |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-09-06 : 09:36:30
|
casati74 -- sounds like you have a really bad database design; table names should never be parameters! I hope this is something that you can change ... with a good design, things are much, much easier and there is no need for dynamic sql. Doesn't it seem like simple things to do are very complicated?You should never have 1 table per employee or customer or something like that; you should have a table of *all* employees or *all* customers, with 1 row for each. - Jeff |
|
|
|