This sql kindly provided by Microsoft lets me see the recently executed stored procedures, but not the values that were passed as parameters.
Where should I look for that information?
SELECT TOP 10
d.object_id, d.database_id,
OBJECT_NAME(object_id, database_id) 'proc name',
d.cached_time, d.last_execution_time, d.total_elapsed_time,
d.total_elapsed_time/d.execution_count AS [avg_elapsed_time],
d.last_elapsed_time, d.execution_count
FROM
sys.dm_exec_procedure_stats AS d
ORDER BY
[total_worker_time] DESC;
That information doesn't really exist anywhere (unless, as others have suggested, you store it yourself). However, you can see it by using SQL Profiler or Extended Events. With SQL Profiler, you need to choose the RPC:Completed and SQL:BatchCompleted events, and make sure that the TextData column is selected for both.
Why not just have you stored procedure insert into someTable on the database.
Something like
INSERT INTO someTable (Value)
SELECT #StoredProcedureInsertParamter
Related
I am trying to convert a SQL Server query to MYSQL, but am having trouble with the IF statements. In the SQL Server version, they are used to direct the flow of the query based on a flag, but I cannot get it to work in MYSQL. I have outlined how the code works below:
The script sets a flag based upon how much data can be matched between the query and the database.
It then performs a select statement based upon the flag: if flag=1 select a,b,c where match logic
else if flag=2 select d,e,f where different match logic
I have tried using both IF and CASE WHEN, neither of which work. I would normally have put the IF within the WHERE clause, but different columns are selected depending upon the flag.
Is there a function that will perform IF/ELSE flows MYSQL?
Any help would be greatly appreciated!
Assuming the types and number of columns are compatible, you could do:
select a, b, c
from t
where (v_flag = 1) and match_condition_1
union all
select d, e, f
from t
where (v_flag = 2) and match_condition_2;
The alternative is to use a stored procedure. In MySQL, the if control flow statement is only allowed in programming blocks -- stored procedures, stored functions, and trigger.
I'm using mysql and writing some queries using SQLYog's query browser. The sql uses a few variables. Below is an example:
SELECT NOW() #cur_dt;
SELECT 'table' INTO #tbl;
SELECT DATABASE() INTO #db;
SELECT ##hostname INTO #host;
SELECT #host AS `host`, #db AS `database`, #tbl AS `table`, #cur_date AS `dt`;
I'm preparing this sql to be used in SSIS 2005(Sql Server Integration Services) as a source. The issue I'm having is that the variables are coming through as blobs instead of varchars or dates.
I can cast each one which works, but my sql above is just a fraction of what I really need which is a bunch of unions. So in the mean time I'm going to wrap a select around all the unions and cast the fields at that point. I know I could put this in a stored procedure and be done with it but I'm wondering about this exact scenario. My question is if there is a way to specify the variable type when declaring?
Thanks,
GG
PS. If you want to punish a developer make them work with SSIS 2005 and mysql.
How about you select all of your data into a table in the mysql query when you have it accumulated? This will give the data reasonable data types. Then use SSIS to interact with the table, not the variables per se.
Example:
http://sqlfiddle.com/#!2/1b059/1
Normally successful execution of
sp_help [object_name]
in SQL Server returns a total of 7 output windows with various results out of which normally I am interested in only 2 windows namely the one with all the column information and the one with the constraints.
Is there a way I can tell SQLserver to only display these while formulating the command?
Short answer: no, you can't do this directly because the procedure is written to return that data, and TSQL has no mechanism for accessing specific result sets.
Long answer: but you can easily get the same information from other procedures or directly from the system catalog:
sp_columns, sp_helpconstraint (this is actually called by sp_help) etc.
sys.columns, sys.objects etc.
There's also the option of copying the source code from sp_help and using it as the basis of a new procedure that you create yourself, although personally I would just write it myself from scratch. If you do decide to write your own stored proc, you might find this question relevant too.
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
I have a stored procedure in SQL Server 2008 that consists of multple select statements. When building a report in SSRS, I have a dataset that uses that stored procedure. However, the only fields that show up are the ones that are in the first select statement. Is there a way to show the other fields or use multiple select statements within one stored procedure?
Thanks!
Do the selects output the same schema (i.e. fields)? If so, and if you need all the results, you could try UNIONing the separate queries together. Otherwise, why not move the query you need into a new stored proc and call that from the report?
Eric, it's been my experience that when SSRS is based on a stored procdure, the results of the last Select statement are used and not the first. So you should be able to do anything you want up until the last Select statement in the stored proc and then make sure the last Select contains the correct data/columns for the report.
From the MSDN documentation
If multiple result sets are retrieved
through a single query, only the first
result set is processed, and all other
result sets are ignored. For example,
when you run the following query in
the text-based query designer, only
the result set for Production.Product
appears in the result pane:
SELECT ProductID FROM Production.Product
GO
SELECT ContactID FROM Person.Contact