March 27, 2014 at 1:29 pm
Hi,
Just looking if somebody has experienced, i have written a tql code query something like, and run ok:
Select Grade, (Count(Grade)* 100 / (Select Count(*) From MyTable)) as Score From MyTable Group By Grade
Is possible that If I embed this code in an expression like:
Set @extsql = ' Select Grade, (Count(Grade)* 100 / (Select Count(*) '
Set @extsql = @extsql+ ' From MyTable)) as Score From MyTable '
Set @extsql = @extsql+ ' Group set By Grade'
Execute (@extsql)
Our final result, were very distinct that when I run this query normally, Are the any constraint when you calculate anything using dynamic sql ?,
Any Idea would be appreciate
March 27, 2014 at 1:38 pm
When you execute the code, SQL Server doesn't care if the code is dynamic or static. As long as the instruction in the dynamic code becomes exactly the same code as your static code.
You should always review your code by printing it when you're testing it.
It might be part of the obfuscation, but your codes aren't exactly the same. That could be happening in your actual query.
March 27, 2014 at 2:41 pm
To me the bigger question is why are you using dynamic sql at all? From what you posted there is absolutely no need for it whatsoever.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 27, 2014 at 2:43 pm
another issue is that query is going to use integer division and return incorrect totals
something in the query needs to be multiplied by 1.0 so that the datatype is expanded beyond integers.
1/3 = 0
1/(3 * 1.0) = 0.33
Select Grade, (Count(Grade)* 100 / (Select Count(*) From MyTable)) as Score
Select Grade, (Count(Grade)* (100 * 1.0) / (Select Count(*) From MyTable)) as Score
Lowell
March 27, 2014 at 2:51 pm
Thanks to all,
Your labor,ideas and toughts inspire ..
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply