Showing posts with label declare. Show all posts
Showing posts with label declare. Show all posts

Wednesday, March 28, 2012

Parameter passing from report to SQL

Hi, I have the following SQL code in my DataSet:
(SQL Server 2005)
DECLARE @.sel1 AS varchar(20)
DECLARE @.sel2 AS varchar(20)
DECLARE @.sel3 AS varchar(20)
DECLARE @.sel4 AS varchar(20)
DECLARE @.test AS integer
set @.test = @.TimeDiff; <-- this I want to insert via URL in report
set @.sel1=convert
(char(10),DATEADD(dd,-convert(integer,@.test),getdate()),121);
set @.sel2=Replace(@.sel1,'-','');
set @.sel3=convert (char(10),DATEADD(dd,0,getdate()),121);
set @.sel4=Replace(@.sel3,'-','');
select @.sel2 AS StartTime, @.sel4 AS EndTime;
I have declared a report parameter (TimeDiff, without @.) in Report
Parameters. It is integer and hidden and has a default value 2 (non queried).
In dataset parameters I do not have any configurations (should I have).
I tried to opass the parameter to my sql code but it is not succeeded.
I got the followng error message:
"Must declare the scalar cariable "@.TimeDiff"
The final meaning is to insert this as parameter via URL and use it in the
dataset sql code, but I can not get it into my SQL .
When running the dataset itself it asks the parameter @.TimeDiff and works OK.
What is wrong in my parameter handling ?Push the ... button (the point button) right beside your dataset
(within the data display), a couple of tabs will popup and goto the
parameters tab.
Within this tab you're able to bind the sql declared parameter to your
reporting parameter.
You'll notice the way it works via your other parameters which are
bound to the sql parameters|||I'd suggest having a stored procedure for this code but as long as you don't
use a temp table (if you use a temp table you must put this in a stored
procedure) then this will work.
I would have expected RS to automatically create a TimeDiff report parameter
to match the @.TimeDiff since @.TimeDiff is not declared. Perhaps it is the
use of the set statement. Try
select @.test = @.TimeDiff
If you click on the ..., parameters tab and there is no @.TimeDiff in the
name column then try adding this by hand and then select your report
parameter that it is mapping to.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"jarmopy" <jarmopy@.discussions.microsoft.com> wrote in message
news:9123A5B9-0DA9-4639-9884-D9D7A30CAA2C@.microsoft.com...
> Hi, I have the following SQL code in my DataSet:
> (SQL Server 2005)
> DECLARE @.sel1 AS varchar(20)
> DECLARE @.sel2 AS varchar(20)
> DECLARE @.sel3 AS varchar(20)
> DECLARE @.sel4 AS varchar(20)
> DECLARE @.test AS integer
> set @.test = @.TimeDiff; <-- this I want to insert via URL in report
>
> set @.sel1=convert
> (char(10),DATEADD(dd,-convert(integer,@.test),getdate()),121);
> set @.sel2=Replace(@.sel1,'-','');
> set @.sel3=convert (char(10),DATEADD(dd,0,getdate()),121);
> set @.sel4=Replace(@.sel3,'-','');
> select @.sel2 AS StartTime, @.sel4 AS EndTime;
> I have declared a report parameter (TimeDiff, without @.) in Report
> Parameters. It is integer and hidden and has a default value 2 (non
> queried).
> In dataset parameters I do not have any configurations (should I have).
> I tried to opass the parameter to my sql code but it is not succeeded.
> I got the followng error message:
> "Must declare the scalar cariable "@.TimeDiff"
> The final meaning is to insert this as parameter via URL and use it in the
> dataset sql code, but I can not get it into my SQL .
> When running the dataset itself it asks the parameter @.TimeDiff and works
> OK.
> What is wrong in my parameter handling ?
>
>
>|||Hi, thanks for the answers.
I tried to put the following parameter setting in the dataset (... button)
@.TimeDiff = Parameter!TimeDiff.Value
but now I get the following error message:
The report parameter 'LastDate' has a DefaultValue or a ValidValue that
depends on the report parameter "TimeDiff". Forward dependencies are not
allowed"
The LastDate report parameter is coming from the dataset from field
StartTime and that is calculated from TimeDiff.
LastTime parameter is Hidden and available value is from query and the
default value is from query.
Still something wrong.|||Hi,
I solved the last problem just by changing the order of the parameters in
report side.
Thanks for nicismyname and Bruce L-C|||Hi, you should be aware of the order of your parameters. If lastdate
parameter it's dataset refers to the timediff parameter you should
first prompt the timediff parameter. The prompting order is determined
by the 'report>parameters' menu. You'll notice two arrows at the
dialog popping up after selecting 'report>parameters', use the up and
down arrow to determine the prompting order.

Parameter not passing on Delete

Interesting problem, selecting and updating (with reference to a Form View) works great. Whenever I try to delete I get this error:

Must declare the variable '@.template_id'.

Description: Anunhandled exception occurred during the execution of the current webrequest. Please review the stack trace for more information about theerror and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Must declare the variable '@.template_id'.

Here is my code:

<asp:SqlDataSource ID="SqlTemplateDS" runat="server"
SelectCommand="SELECT [template_id], [client_id], [language], [label], [email_default], [email_template], [event_template] FROM [CMT] ORDER BY [template_id]"
DeleteCommand="DELETE FROM CMT WHERE (template_id = @.template_id)"
ConnectionString="<%$ ConnectionStrings:BrandMSConnectionString %>">
</asp:SqlDataSource
<asp:GridView ID="grdTemplates" runat="server" AutoGenerateColumns="False"
DataKeyNames="template_id"
DataSourceID="SqlTemplateDS"
Font-Names="Verdana"
Font-Size="Small"
ForeColor="Black"
Width="100%"
OnSelectedIndexChanged="grdTemplates_SelectedIndexChanged"
CellPadding="2">
<Columns>
<asp:BoundField DataField="template_id" HeaderText="ID" ReadOnly="True" SortExpression="template_id" />
<asp:BoundField DataField="client_id" HeaderText="Client" SortExpression="client_id" />
<asp:BoundField DataField="language" HeaderText="Language" SortExpression="language" />
<asp:BoundField DataField="label" HeaderText="Label" SortExpression="label" />
<asp:CheckBoxField DataField="email_default" HeaderText="Default" SortExpression="email_default" />
<asp:CheckBoxField DataField="email_template" HeaderText="Email" SortExpression="email_template" />
<asp:CheckBoxField DataField="event_template" HeaderText="Event" SortExpression="event_template" />
<asp:TemplateField HeaderText="Options">
<ItemTemplate>
<asp:LinkButton ID="btnModify" CommandName="Select" runat="server" ForeColor="Blue">Edit</asp:LinkButton> |
<asp:LinkButton ID="btnDelete" CommandName="Delete" runat="server" ForeColor="Blue">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="Khaki" />
<AlternatingRowStyle BackColor="Beige" />
</asp:GridView>

Any ideas?

ChrisI solved the problem by using the "original_" prefix in front sql variable so it reads:

DeleteCommand="DELETE FROM CMT WHERE (template_id = @.original_template_id)"

I think its pretty poor that there wasn't better documentation regarding this issue. Apparently ifany of the items in DataKeyNames areshown in a databound column, updating and deleting actions need to specificy whick key value to use. In this case using the prefix "original_" signals to the control which parameter value to use.

I hope this helps anyone who is having similar problems in the future.