Click here to Skip to main content
15,885,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello there......
i need to create reports in report builder. Sql Query is same for every report but the column name of table changes report to report, For That i created procedure as follows

SQL
alter proc sp_testdynamiccolname
(
    @columnname nvarchar(100),
    @date nvarchar(10)
)
as
begin
declare @query nvarchar(4000)
set @query='Select max('+@columnname+') as maxval,datepart(hour,timestamp) as hour from pointdb where convert(varchar,timestamp,101) in('+Convert(varchar,@date,101)+') group by datepart(hour,timestamp)'

exec(@query)
end


exec sp_testdynamiccolname 'SOLAR_ACTIVE_POWER_G_VAL0','03/07/2014'

but it shows an error:-

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '03/04/2014' to data type int.


plz suggest solution.
Posted

1 solution

Hi,

You're definitely missing quotes here. You could use print @query in your stored procedure in order to see how @query looks like. It looks like this:
SQL
Select max(SOLAR_ACTIVE_POWER_G_VAL0) as maxval,datepart(hour,timestamp) as hour from pointdb where convert(varchar,timestamp,101) in(03/07/2014) group by datepart(hour,timestamp)

In order to fix this error, your code near @date should be like this:
SQL
in('''+Convert(varchar,@date,101)+''')


Optional information:
There is another way to write dynamic SQL. It might be useful to you. Please take a look at these links:
1. sp_executesql (Transact-SQL)[^]
2. EXEC and sp_executesql – how are they different?[^]
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900