How to pass parameters to procedure in SSRS? - ssrs-2008

I have a table like this:
create table emp(empid, empname, sal)
insert emp values(101, 'srewt', 1500)
The table contains 10000 rows.
I also have a stored procedure like this:
create procedure p1 (#eid int)
as
begin
select * from emp where empid = #eid)
end
In SSRS I execute this procedure
exec p1 (' ')
but I have to provide user interaction to the report to pass empid value (nothing but parameter)
How can I pass a parameter to the report?

You can define the parameter in the report designer. Open the report data tab (should be in the same place, as the solution explorer. if it is not there, you can show the report data dock by opening a report, clicking on View-ReportData).
When you double click on the report dataset, you can configure the report parameters. If you chose a stored procedure with parameters as datasource, the designer should detect the parameters of the procedure automatically and fill in the values in the parameter tab for you. But make sure to check the data type, as the detected data types often are not the ones you really need.

In your dataset just pass exec p1 (#EmpID) as a query, then automatically a parameter with name EmpID will be created in Report. Here when user run the report it asks for EmpID to enter, just give the ID and run the report...

Related

How To Add More Than 1 Select Sections as DataSource in Crystal Report

As below procedure it has two select sections (type=1 and type=2). I am binding Crystal Report with this procedure. Both the sections has to bind to report. type=1 gives multiple rows & type=2 gives single row. First i have to bind report with type=2 then with type=1 on same report. But on selecting this procedure as DataSource in Crystal Report it is picking only type=1 select method as DataSource. How should I include both types in procedure as DataSource in Report?
I am using CrystalReport in VS2008.
ALTER procedure [dbo].[usp_report]
#applicationno varchar(20)=null,
#sessionyr varchar(10)=null,
#type int=null
as
BEGIN
if(#type=1)
begin
/* Select Statement Goes Here */
end
if(#type=2)
begin
/* Select Statement Goes Here */
end
END
It's not possible to use more than one resultset from a stored proc in Crystal. You could adjust your proc, so that it delivers same columns for both types (and fill only the used ones, of course).

Is there a right-click way to INSERT INTO SELECT in SSMS, or is the menu tweakable?

I'd like to know whether it is possible to do a insert into select statement with the use of right-click in SQL Server Manegement Studio (SSMS). And if not, is that tweakable?
When right-clicking on a table in the database (example here, I know I can right-click the target table, select 'Script Table as'/'Select to',paste the script,right-click the source-table, select 'Script Table as'/'Select to' and paste that in the just pasted piece of script instead of the VALUES-part. I want somehow to bypass these steps and possibly there is a simple way to do it already. But then I have to see 'Insert into select', or something. I'd like SSMS to generate for me a script something like this:
USE [DatabaseName]
GO
INSERT INTO [TableAA_New]
([A]
,[B]
,[C])
SELECT [A]
,[B]
,[C]
FROM [TableAA]
GO
So, my question is whether there is some right-click option that I don't see?
If there's not, is there some way of adding such a possibility to the right-click options? Can I tweak those? Actually a kind of extending the right-click menu in SSMS... Can anyone tell me or get me in the right direction?
Not 100% this is what you mean, But yes you can
if you have
SELECT * FROM MyTable
Select it then right click it. You get a context menu with an option "Design Query in Editor..."
This pops up a UI for building SQL. At the top of this window is a section where you can see the tables involved. If you right click on the white space in this top window you get another context menu with "Change type" this allows you to change your statement into an insert statement.
If you select insert values then you can input values that you want to insert into the table (on the UI)
But i expect you want "insert results". Selecting this means you then need to select the target table. You then need to map values from one table to another by updating the append column on the UI. If the target tables has the same columns as the source then the append column will be automatically updated to match the two columns.
Once done you get
INSERT INTO OtherTable
(A,B,C)
SELECT A,B,X
FROM MyTable
Hope this helps
Based on the answer #Paul-Spain gave, see beneath a way to make insert into select statement with (right-)mouse clicks. In effect you use Design Query in Editor.
Beneath is described how to use Design Query to insert into select from source table MyTable to target table OtherTable.
First a piece of script to get you started, e.g. to make MyTable and OtherTable, and to fill MyTable.
CREATE TABLE [dbo].[MyTable](
[A] [nvarchar](100) NULL,
[B] [nvarchar](100) NULL,
[C] [nvarchar](100) NULL
)
CREATE TABLE [dbo].[OtherTable](
[A] [nvarchar](100) NULL,
[B] [nvarchar](100) NULL,
[C] [nvarchar](100) NULL
)
insert into MyTable values (1,2,3),(4,5,6),(7,8,9);
1: Right-click somewhere in query window
2: A pop-up appears. Select the table you want as SELECT-table.
3: Right-click somewhere, select Change Type, Insert Results. So you actually give order to the dbms to insert the results of the curent query, that is based on MyTable. So you say to insert that somewhere. Where to insert it you define in a next window.
4: Select the table where to insert it too. If you don't have the table yet, you will have to make it. (That's also possible in this editor).
5: Tell the dbms which columns to select.
6: Run the generated script.
7: The data is inserted in OtherTable. Done.

How to update an XML column in DB2 with XMLQuery?

I want to update an XML column in DB2 with dynamic values or you can say with values that I'll pick from another table and insert them in the xml column.
I know how to insert a node along with its value that we provide by
hard coding it, e.g.
<data>some_value</data>
I want to do it in the following way:
UPDATE my_table SET my_table_column = XMLQuery(..... <data>???</data>)
WHERE my_table_id = other_table_id;
Where I place ??? I need a kind of select statement here which will come up with actual value for the node.

Stored procedure get column information does not return anything?

I am using entity framework with a stored procedure, in which I am generating query dynamically and executing that query. The stored proc query looks like:
Begin
DECLARE #Query nvarchar(MAX)
SET #Query = 'SELECT e.id, e.name, e.add, e.phno from employee'
EXEC sp_executesql #Query
End
In above sql code you can see that i am executing '#Query' variable, and that variable value can be changed dynamically.
I am able to add my stored proc in my edmx file. and then I go to model browser and say Add function import and try to Get column information it does not show anything. but when I execute my stored proc at server it returns all columns with values. Why i am not getting column information at model browser?
The model browser isn't running the stored procedure to then gather the column information from its result - it's trying to grab the column information from the underlying procedure definition using the sys tables.
This procedure, because it's dynamic, will not have an underlying definition and therefore won't be importable into the EDMX like this.
Temporarily change your stored proc to
SELECT TOP 1 e.id, e.name, e.add, e.phno from employee /* ... rest of your proc commented out */
Then add it to the EF and create the function import (it will see the columns).
Then change the proc back to how you had it above.
Try adding SET NOCOUNT ON after BEGIN.... that supresses messages that might cause it to be "confused"

Simple Way to View SQL Query (ies) Generated by SSRS Reports?

Is there a simple way to view the SQL Queries actually generated by SSRS other than running profile traces to capture them?
Is there some way from within the BIDS editor to see this?
You could run something like the below against your SSRS report server. You will be able to see the sql that is being execute by the report datasets.
;WITH XMLNAMESPACES (
DEFAULT 'http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition',
'http://schemas.microsoft.com/SQLServer/reporting/reportdesigner' AS rd
),
ReportData AS
(
SELECT name ReportName
, x.value('CommandType[1]', 'VARCHAR(50)') AS CommandType
, x.value('CommandText[1]','VARCHAR(8000)') AS CommandText
, x.value('DataSourceName[1]','VARCHAR(50)') AS DataSource
FROM (SELECT name
, CAST(CAST(content AS VARBINARY(MAX)) AS XML) AS reportXML
FROM ReportServer.dbo.Catalog
WHERE content IS NOT NULL
AND type != 3) a
CROSS APPLY reportXML.nodes('/Report/DataSets/DataSet/Query') r(x)
)
SELECT *
FROM ReportData
In short, no. There is no good workaround. However, for development I generally created a test query alongside my work in SSRS. I would edit this inside Management Studio and then just paste the values back into BIDS. Assuming two parameters named "StudentID" and "TeacherID", the query looked like:
DECLARE #StudentID int
DECLARE #TeacherID int
SELECT #StudentID = StudentID FROM Students WHERE StudentName LIKE 'John Doe'
SELECT #TeacherID = TeacherID FROM Teachers WHERE TeacherName LIKE 'Mr. Jones'
-- PASTE IN QUERY FROM BIDS BELOW
This allowed me to use real text values from the drop-down parameter lists and simply paste in my query. Then I could optimize the query in Management Studio and then paste it back into BIDS when I was happy with the result.
What I normally do is run SQL Profiler when I run the report and pull the query out of it with the parameters.
Close the file, change the extension from .rdlc to .rdl and reopen it. It should display as HTML. Now do a search for "select" and there you go!