Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm doing a couple of tests regarding speed in SQL Server. To do it I have this simple query:
DECLARE #Time1 Date;
DECLARE #Time2 Date;
Set #Time1 = GETDATE();
-- Stuff happening
Set #Time2 = GETDATE();
Now I want to check the values of these properties (Time1 and Time2). I tried doing a SELECT #Time1, Time2 but kept getting errors, so how can I check these values?
EDIT: as I mentioned to the accepted answer author, it was probably some syntax error that was eluding me from using a simple SELECT.
As for the fact the question is On Hold, I really don't see why as it seems a simple question to me. I created #parameters and set a value to them, then I want to check these values.
"Doing a SELECT is not possible" - Who told you this?
DECLARE #Time1 DateTime;
DECLARE #Time2 DateTime;
Set #Time1 = GETDATE();
-- Stuff happening
Set #Time2 = GETDATE();
SELECT
#Time1
,#Time2
I've used this when analysing a large stored procedure, I put some datetime parameters at key points within the stored proc using getdate() and inserted them into an audit table at the end of the procedure. It was really handy for determining which part of the live stored procedure was going slowly for users.
First, perhaps, it is nessecarry to declare the #TimeX as TIME values - not as DATE ....
and than you con use PRINT
Print #Time1
Print #Time2
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a stored procedure that runs a few times a day in SQL Server 2016.
It is nothing more than a few selects that sum a value and count the records. It returns the values as output parameters.
Every so often, maybe once every two months it will just stop working. I will end up getting a time out issue when calling if from code (C#).
Normally it executes in less than one second. If I recompile it, everything returns to normal and I wont have another issue for months.
Any idea what could be causing this?
What additional information can I provide to assist in discovering the issue?
Here is a sample of one of the queries:
SET ANSI_NULLS ON
ALTER PROCEDURE [dbo].[spName]
#dateFrom as datetime,
#dateTo as datetime,
#CustID as varchar(50)
AS
BEGIN
SET NOCOUNT ON;
declare #TotalReturns as decimal(12,0),
#TotalReturnsValue as decimal(12,2),
--Total Transactions and Value for returns within date range
--**********************************************************
select #TotalReturns = count(*), #TotalReturnsValue = sum(cast(isnull([amountField],'0') as decimal(12,2)))
from [tableName]
where [customerField]=#CustID
and [returnDateField] between #dateFrom and #dateTo
and replace([amountField],' ', '') != ''
--**********************************************************
--Outputs
select isnull(#TotalReturns,0) as TotalReturns,
isnull(#TotalReturnsValue,0) as TotalReturnsValue
END
Based on the sample query and the discussion in comments I am pretty sure this is related to parameter sniffing and sub-optimal plans based on data skew across clients and date ranges. You can read about parameter sniffing in depth here. https://www.sqlinthewild.co.za/index.php/2007/11/27/parameter-sniffing/. Gail does a great job explaining the performance challenges with this and a number of ways to combat it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How to call one stored procedure from another stored procedure and when I do that. Then it is not updating in Crystal Report field explorer.
This is my code:
SP1
ALTER PROCEDURE [dbo].[GetSpecialJournalInfoByJVID]
(#JvID numeric,#BusinessID numeric)
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM JV
WHERE jvID = #JvID AND BusinessID = #BusinessID
END
SP2
ALTER PROCEDURE [dbo].[USP_GetSpecificAccountLedger]
#JvID numeric(18,0),
-- Add the parameters for the stored procedure here
#ParamDate1 datetime,
#ParamDate2 datetime,
#ParamBusinessID numeric(18,0),
#ParamAccountID numeric(18,0),
#ParamCurrentCurrencyRate decimal(18,10) =1
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT *
FROM JV
WHERE jvID = #JvID
-- Insert statements for procedure here
SELECT
COA.ACCOUNTID, COA.ACCOUNTNAME, COA.GLCODE,
COA.DESCRIPTION AS ACCOUNTDESCRIPTION,
COA.ISBANKACCOUNT, COA.BANKACCOUNTNO,
COA.REFACCOUNT, FIXED,
ISNULL(OPBAL.OPENINGBALANCE, 0) OPENINGBALANCE,
ISNULL(CURBAL.DR_CUR_BAL, 0) DR_CUR_BAL,
ISNULL(CURBAL.CR_CUR_BAL, 0) CR_CUR_BAL,
J.TRNDATE, J.TRNDESCRIPTION, TRNTYPE,
I need help, I googled it a lot but didn't got any solution
thanks in advance...
You can call a stored procedure by using the EXEC command. However, it might be better to use SQL Server user-defined function.
Please see: https://technet.microsoft.com/en-us/library/aa175085(v=sql.80).aspx
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
SP definition with Return Parameter
if OBJECT_ID('CountSeatleEmployee') is not null
drop proc CountSeatleEmployee
go
CREATE PROC CountSeatleEmployee
AS
DECLARE #Total int
SELECT #Total =Count(*)
from [AdventureWorks2012].[Person].[Person] P
WHERE P.EMAILPROMOTION =0
RETURN #Total
--Execute SP
declare #Count int
exec #Count = CountSeatleEmployee
SELECT #COUNT
--11158 Records
--same SP without Return Parameter
if OBJECT_ID('CountSeatleEmployee') is not null
drop proc CountSeatleEmployee
go
CREATE PROC CountSeatleEmployee
AS
SELECT Count(*)
from [AdventureWorks2012].[Person].[Person] P
WHERE P.EMAILPROMOTION =0
exec CountSeatleEmployee'
Now why not simply use below . These two are providing same output. Why do we seed return parameter
In my experience, return values are usually used for error conditions. Not everyone has the same experience.
The RETURN statement is limited to returning an integer so it's usefulness is somewhat limited (in my opinion). I prefer the second method because it allows for more consistency in your code. Imagine you wanted a name instead of a count, you would not be able to use the return statement.
On the other hand, some people prefer the first method that uses the return statement (wherever it is possible to do so). The justification is that it is an optimization technique. When you call the first code example from your front end, there is no need to process the procedure with the expectation that it would return a result set. Instead, it's just a single 4 byte value passed back from the database. The second example requires more processing on the front end because a procedure can return all sorts of things. It could return multiple result sets and each result set could contain multiple columns and multiple rows. Your front end code needs to evaluate the data coming back and then build the proper structure for handling the data. This takes extra CPU cycles.
I'm not necessarily recommending one over the other, I'm just trying to explain the rationale.
The one way provides the information in a return variable which you then convert into a recordset with the Select of the variable, the other as a dataset or recordset. It depends on the application which one you want to use.
if OBJECT_ID('CountSeatleEmployee') is not null
drop proc CountSeatleEmployee
go
CREATE PROC CountSeatleEmployee
AS
DECLARE #Total int
SELECT #Total =Count(*)
from [AdventureWorks2012].[Person].[Person] P
WHERE P.EMAILPROMOTION =0
Select * from [AdventureWorks2012].[Person].[Person] P
WHERE P.EMAILPROMOTION =0
RETURN #Total
--Execute SP
declare #Count int
exec #Count = CountSeatleEmployee --This will return the recordset of the table and returns the total rows into the variable #Count
SELECT #COUNT
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a query like this:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[User_SelectByLoginID]
#LoginID nvarChar(4)
as
SELECT dbo.[User].*
FROM dbo.[User]
WHERE LoginID=#LoginID
And data in the User table:
LoginID ='1111' | Name ='abc' | Email = 'abc#yahoo.com'
when I executed this query and typed in '1111111', it returned the record:
1111 abc abc#yahoo.com
it is ridiculous when I entered the wrong LoginID and still got the data.
P/S: I set LoginID nvarchar(4)
Can someone explain for me? And how to make it right?
If you set #LoginID to nvarchar(4) it will truncate to that size so really you are passing in 1111 and not 11111111.
SQL Server silently truncates your value passed to stored procedure, so even though you pass value '1111111', it is cut off to the declared length (4) so in your stored procedure there is a value '1111'.
So you should declare your parameter #LoginID to the same size which has your column LoginID in User table
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am using 2008 R2 ,I want to set output of "Select Colname from tablename" to one veriable ie. #resultvar ,so further I can use it in stored procedure.
If you are only interested in a single value, it is as simple as:
DECLARE #resultVar nvarchar(50) -- or whatever
SELECT #resultVar = Colname FROM tablename -- with appropriate filtering
If you're needing to get this out from a stored procedure, then you need to look into OUTPUT parameters:
CREATE procedure MyProcedure
#resultVar nvarchar(50) OUTPUT
AS
SELECT #resultVar = Colname FROM tablename -- with appropriate filtering
If you're using SQL to run the procedure, you can then use:
EXEC MyProcedure #var OUTPUT
Obviously, if you're using a framework such as Entity Framework, then you need to use whatever that framework provides; but, that's a totally separate question.
Be aware that if multiple rows come back from tablename, you will only get the value from the first row. If it is more complex than this, please expand the original question.
You can use a simple code like this:
DECLARE #resultvar AS VARCHAR(64) --or any other type you need
SELECT #resultvar = ColName FROM TableName WHERE --filter on table
PRINT #resultvar