Can I write a stored procedure in Query Builder within SSRS? - sql-server-2008

I want to run the following procedure first:
exec ep_upd_server_abc
Then execute the following statement:
SELECT * FROM dbo.Server_Warehouse
WHERE Enabled = 'f' and disabledweekly = 'f'
Can I write the following procedure in SSRS Query Builder so that I can execute two statements?
CREATE PROCEDURE ShowDBJob
As
Begin
exec ep_upd_server_abc;
SELECT * FROM dbo.Server_Warehouse
WHERE Enabled = 'f' and disabledweekly = 'f'
End
Is there a work around for this?
I tried running this in SSRS Query Builder and it's giving me an error. Would appreciate any thoughts and suggestions...Thanks!!

Why not just do this exact procedure for
CREATE PROCEDURE ShowDBJob
As
Begin
exec ep_upd_server_abc;
SELECT * FROM dbo.Server_Warehouse
WHERE Enabled = 'f' and disabledweekly = 'f'
End
in SQL Management Studio and call that? Don't do any type of real time event driven coding or procs in SSRS IMHO. Slippery slope where you are trying to use the equivalent of a screwdriver for a hammer and vice versa. SQL Managment studio is meant for designing proc's and accessing the SQL Server Database for it's object creation directly. SSRS is meant to return data connections, results for a dataset, evaluating presentation of datasets on one or many objects to display and not much more except features/syntax around those things. Even accessing temp tables '#(something)' and parameters declared on the fly 'declare #thing int (inside statement of dataset)' make it get mad. Stick with predefined programable objects or procedures and functions already created. Do not try to create them on the fly.
Plus think about this. Everytime you are calling your dataset you cannot create that object each time as it already exists. Then you are going to have to either alter it or drop it and recreate it first. SSMS already does this part in options for you and stores them in the database. SSRS is an add on to SQL Server but is not full featured for creating objects. Especially on the fly.

No, You cannot write "Create Procedure" or any other DDL statements from Query Designer in RS. This is by design.

Related

sql server stored procedures in jaspersoft studio 5.6

I have a stored procedure my_sp which takes in two parameters, first an integer and then a date. That is,
EXEC my_sp 265522,'6-10-15'
I have been trying to design a report based on the result set from this procedure in Jaspersoft Studio 5.6. The report does not generate or atleast takes way too long if I use the two parameters though the output data consists of only 25 rows. I am using this query,
exec my_sp $P{param1} , $P{param2}
and feel the probelm is with the syntax only. Also the same query works perfect when I had tried it in Sql Server Management Studio and in jaspersoft studio, stored procedures with a single parameter work like magic. Kindly help me out on this
This should absolutely work.
Add an ! after the P of each parameter
Is the SP creating temp tables? If so try another driver OR create another SP that returns the result of this SP
Try another driver anyway
add the #paramname from tsql = $P{} instead of anonymously naming them

How do I save a stored procedure in SQL Server 2008 R2?

I am writing a SQL Server stored procedure for the first time and am unclear on how I can "save" my stored procedure so that it appears under Programmability, Stored Procedures in the Object tree.
The CREATE PROCEDURE procedureName statement creates the procedure.
You just need to execute it once and it will save the procedure to your database.
Make sure to select the correct database you want to save the procedure to, either by selecting it in the top left hand corner of SQL Server Management Studio, or by putting the following at the top of your code:
USE databaseName
Also note, if there are any syntax errors, it won't "save" the procedure.
While you are learning SQL Server and Management Studio, you may find it very helpful to become familiar with the built-in templates for creating everything from databases to tables to stored procedures and more. You locate the templates in Template Explorer under the View menu.
The first example in this walk-through with screenshots shows how to use the template for creating a stored procedure. That template includes a placeholder for the schema name (often just dbo).
You will also want to include a USE statement to make sure that the stored procedure is created in the correct database.
In addition to helping you to learn proper coding practice, using these templates can be a real time-saver and help you to avoid typos and syntax errors even after you becomem proficient in SQL.
And when you get really good at it, you can create your own templates.
Edit: Here is a very basic CREATE PROCEDURE statement:
USE MyDatabase
GO
CREATE PROCEDURE dbo.MyProcedure
AS
SELECT FirstName, LastName, Address, City
FROM Customers
ORDER BY LastName
GO
After you run that, you can run this line to check that the procedure has been created and that it is working correctly:
EXEC dbo.MyProcedure
you just use a create statement:
http://msdn.microsoft.com/en-us/library/aa258259(v=sql.80).aspx

Return variable number of columns in SQL Server 2008

My table contains the following fields, Name,Age,Salary,Phone,DOB. Based on a settings table, I have to select only some fields. For example, I say in settings, only Name and Phone is required. How can I do it using stored procedure ?
EDIT :
Which one is good.
Select the required fields from the table.
Select all columns and in ASP.NET page, use .Visibility property to hide or show columns
SQL is a fixed column language: columns can not be added or removed "on the fly"
You would need to use dynamic SQL to build a SELECT statement, or use IF statements to execute different ones. However, you open up caching, security and injection issues.
Personally, I'd ignore columns in the client code and have a simple, single efficient SQL query. The contract or API between SQL Server and the client should be static and predictable. If the settings table is applied in SQL Server, your client doesn't know what columns to expect. If your client does know, then it can ignore them.
After your edit, option 2, kind of.
But the data should be removed before being rendered in the page.
Keep it simple: don't try to optimise anything yet
You would need to have multiple different selects - based on your settings table - in your stored proc to return the different sets of data.
CREATE PROCEDURE dbo.YourProcedure(...)
AS BEGIN
DECLARE #Setting INT -- ?? whatever it is
SELECT #Setting = Choice FROM dbo.YourSettingsTable WHERE ....... ???
IF #Setting = 1
SELECT Name, Phone
FROM dbo.YourDataTable
ELSE
SELECT Name, Age, DOB, Phone, Salary
FROM dbo.YourDataTable
END
Using this approach, however, has its dangers - since the stored proc might return one set of data or quite another, your SQL Server query optimizer might make a very good decision on how to access the data for one setting - but when your setting changes, that execution plan will be totally outdated, thus potentially leading to horrible performance......
On the other hand - it might be easier to determine that setting before calling your stored proc - and then just pass in that setting as a stored proc parameter.
Or even better yet: have separate stored procs for each "scenario" - and then from the caller, call the appropriate stored proc depending on the value of your setting....
Create the sql you want dynamically then execute it with exec.
declare #sql varchar(500)
set #sql = 'select 123'
exec (#sql)
The above code should help you understand what you need to know.
Have a stored procedure for each set of fields you want to select
Allow the list of field names to be passed in as a parameter

How do I make a stored procedure in MS Access?

How do I make a stored procedure in MS Access?
Access 2010 has both stored procedures, and also has table triggers. And, both features are available even when you not using a server (so, in 100% file based mode).
If you using SQL Server with Access, then of course the stored procedures are built using SQL Server and not Access.
For Access 2010, you open up the table (non-design view), and then choose the table tab. You see options there to create store procedures and table triggers.
For example:
Note that the stored procedure language is its own flavor just like Oracle or SQL Server (T-SQL). Here is example code to update an inventory of fruits as a result of an update in the fruit order table
Keep in mind these are true engine-level table triggers. In fact if you open up that table with VB6, VB.NET, FoxPro or even modify the table on a computer WITHOUT Access having been installed, the procedural code and the trigger at the table level will execute. So, this is a new feature of the data engine jet (now called ACE) for Access 2010. As noted, this is procedural code that runs, not just a single statement.
If you mean the type of procedure you find in SQL Server, prior to 2010, you can't. If you want a query that accepts a parameter, you can use the query design window:
PARAMETERS SomeParam Text(10);
SELECT Field FROM Table
WHERE OtherField=SomeParam
You can also say:
CREATE PROCEDURE ProcedureName
(Parameter1 datatype, Parameter2 datatype) AS
SQLStatement
From: http://msdn.microsoft.com/en-us/library/aa139977(office.10).aspx#acadvsql_procs
Note that the procedure contains only one statement.

Is it possible to determine by way of T-SQL Query whether a sproc is CLR-based or regular T-SQL-based?

Hey all. I'm integrating a CLR/Assembly-based sproc replacement for an existing sproc that lives within our production database, and I want to write an update script that only drops the old T-SQL-based script and doesn't drop the new one if its already there. Is that possible?
When you query against sys.objects where type = 'P' (or sys.procedures), only SQL stored procedures are returned and not CLR functions. See sys.objects or more.