Showing posts with label section. Show all posts
Showing posts with label section. Show all posts

Wednesday, March 28, 2012

Profile / Stored Procedure

Hi,

I have created a Stored Procedure, under Stored procedures section under Enterprise Manager on SQL server 2000.

Could anybody tell me, how and what are the steps to follow to TRACE the procedure , using SQL PROFILER ?

Please advice me !

NicolFrom Enterprise Manager:
-- choose Tools
-- SQL Profiler
-- File
-- New
-- Trace
-- select/enter connection informatin as appropriate (I was able to accept the defaults)
-- press OK
-- click the General tab of the Trace Properties window
-- from the "Template name:" dropdown choose SQLProfilerTSQL_SPs
-- choose RUN
-- do whatever it is you need to do to run your Stored Procedure (from within Query Analyzer or ASP.NET)
-- go back to Profiler and click the red square "stop selected trace" icon
-- review the results

Terri|||Thanks Tmorton,

I got started with checking SP using SQL profiler.. :)

Now I found, I think, I have problem with coding in VB.NET or problem in SP.

Even though, I have given valid Username/Password, RETURNVALUE is always showing zero .

Stored procedure
-------
CREATE PROCEDURE test
(@.username varchar(100),
@.userpwd varchar(200)
)
AS
set nocount on
return (select count(*) as s from employer
WHERE user_name like '%@.username%' and
password ='@.userpwd')

GO

VB.NET Code using SP
--------
CmdCoInfo = New SqlCommand("test", ConVM)
CmdCoInfo.CommandType = CommandType.StoredProcedure
CmdCoInfo.Parameters.Add("@.username", Trim(StrUser))
CmdCoInfo.Parameters.Add("@.userpwd", Trim(StrPass))
Dim paramOut As SqlParameter
paramOut = CmdCoInfo.Parameters.Add("Returnvalue", SqlDbType.Int)
paramOut.Direction = ParameterDirection.ReturnValue
paramOut.Size = 40
' Retrieve the record that matches the username/password
ConVM.Open()
CmdCoInfo.ExecuteNonQuery()

Dim retrr As String
If Not IsDBNull(CmdCoInfo.Parameters("Returnvalue").Value) Then
retrr = (CmdCoInfo.Parameters("Returnvalue").Value)
Else
retrr = "Not know"
End If
Response.Write(retrr)

--------------

Is there problem with SP or VB.net code ?

Please advice !|||The problem is in your stored procedure.

Instead of this:

return (select count(*) as s from employer
WHERE user_name like '%@.username%' and
password ='@.userpwd')
GO

This would be more correct:


DECLARE @.RecordCount integer
select @.RecordCount= count(*) from employer
WHERE user_name like '%'+@.username +'%' and
password =@.userpwd
RETURN @.RecordCount

GO

HOWEVER, ReturnValue is meant to signal back to the calling program whether or not the stored procedure was successful. So you are misusing it to return a COUNT. You should use an Output parameter instead. Add an additional parameter called RecordCount with ParameterDirection of Output. Add the Output parameter to your stored procedure, which will look like this:

CREATE PROCEDURE test
(@.username varchar(100),
@.userpwd varchar(200) ,
@.RecordCount int OUTPUT
)
AS
set nocount on
select @.RecordCount= count(*) from employer
WHERE user_name like '%'+@.username +'%' and
password =@.userpwd
RETURN @.@.ERROR

GO


I haven't tested this code but it should be about right.

Terri|||Terri,

ur 2nd option worked for me. But i didnt understand why I am still getting zero as ReturnValue , when I tried with 1st option(given below), eventhough record exists for that username/pwd.

DECLARE @.RecordCount integer
select @.RecordCount= count(*) from employer
WHERE user_name like '%'+@.username +'%' and
password =@.userpwd
RETURN @.RecordCount
GO

Please advice !|||I used that same stored procedure, and your code modified slightly which executes the procedure and puts the returnvalue into a label and it works as expected:


CmdCoInfo = New SqlCommand("returntest", SqlConnection)
CmdCoInfo.CommandType = CommandType.StoredProcedure
CmdCoInfo.Parameters.Add("@.test1", 1)
Dim paramOut As SqlParameter
paramOut = CmdCoInfo.Parameters.Add("Returnvalue", SqlDbType.Int)
paramOut.Direction = ParameterDirection.ReturnValue
paramOut.Size = 40

SqlConnection.Open()
CmdCoInfo.ExecuteNonQuery()

Label1.Text = CmdCoInfo.Parameters("ReturnValue").Value

Terrisql

Monday, February 20, 2012

Procedure Stopping

Hi,

Please look at the code at the bottom of this procedure (UPDATE
COVENANT PRINT DATE). When I put it at the tope of this section, it
does the update and fails to return the SELECT results. When I put it
at the bottom, the SELECT returns results and the UPDATE fails.

Can anyone suggest what might be causing my problem?

Thanks

/************ SPECIFIC PROGRAM SEARCH ************************/
IF @.PRG_ID != 0 -- PRG_ID supplied
BEGIN
SET @.strWHERE = 'WHERE PRG.PRG_ID = ' + cast(@.PRG_ID as varchar(15))
EXECUTE (@.strSELECT + ' ' + @.strFROM + ' ' + @.strWHERE + ' ' +
@.strORDERBY)
RETURN(0)
END

/************ NON-SPECIFIC PROGRAM SEARCH ************************/

IF @.ORG_ID != 0 -- Add ORG_ID to where
IF @.intAND = 0
BEGIN
SET @.strWHERE = @.strWHERE + 'ORG.ORG_ID = ' + cast(@.ORG_ID as
varchar(15))
SET @.intAND = 1
END
ELSE
BEGIN
SET @.strWHERE = @.strWHERE + ' AND ORG.ORG_ID = ' + cast(@.ORG_ID as
varchar(15))
END

IF @.PRG_Closed != 2 -- Add PRG_Closed to where
IF @.intAND = 0
BEGIN
SET @.strWHERE = @.strWHERE + 'PRG.PRG_Closed = ' + cast(@.PRG_Closed
as varchar(15))
SET @.intAND = 1
END
ELSE
BEGIN
SET @.strWHERE = @.strWHERE + ' AND PRG.PRG_Closed = ' +
cast(@.PRG_Closed as varchar(15))
END

IF @.strWHERE != 'WHERE '-- if some parameters supplied..
BEGIN-- Execute search
EXECUTE (@.strSELECT + ' ' + @.strFROM + ' ' + @.strWHERE + ' ' +
@.strORDERBY)
RETURN(0)
END

/************ UPDATE COVENANT PRINT DATE ************************/
-- Added by JSHAW 02/25/2004
IF @.PRG_Covenant != 0 -- PRG_Covenant supplied

BEGIN
--SET @.ORD_Date = GETDATE()
UPDATE PROGRAMS
SET Covenant_Printed = convert(char(10),@.ORD_Date,101)
WHEREPRG_ID = cast(@.PRG_ID as varchar(15))
END
GO"RETURN is immediate and complete and can be used at any point to exit from
a procedure, batch, or statement block. Statements following RETURN are not
executed."

HTH
Igro Raytsin

"John Shaw" <jmshaw@.weir.net> wrote in message
news:91422298.0403041138.1059d960@.posting.google.c om...
> Hi,
> Please look at the code at the bottom of this procedure (UPDATE
> COVENANT PRINT DATE). When I put it at the tope of this section, it
> does the update and fails to return the SELECT results. When I put it
> at the bottom, the SELECT returns results and the UPDATE fails.
> Can anyone suggest what might be causing my problem?
> Thanks
> /************ SPECIFIC PROGRAM SEARCH ************************/
> IF @.PRG_ID != 0 -- PRG_ID supplied
> BEGIN
> SET @.strWHERE = 'WHERE PRG.PRG_ID = ' + cast(@.PRG_ID as varchar(15))
> EXECUTE (@.strSELECT + ' ' + @.strFROM + ' ' + @.strWHERE + ' ' +
> @.strORDERBY)
> RETURN(0)
> END
> /************ NON-SPECIFIC PROGRAM SEARCH ************************/
> IF @.ORG_ID != 0 -- Add ORG_ID to where
> IF @.intAND = 0
> BEGIN
> SET @.strWHERE = @.strWHERE + 'ORG.ORG_ID = ' + cast(@.ORG_ID as
> varchar(15))
> SET @.intAND = 1
> END
> ELSE
> BEGIN
> SET @.strWHERE = @.strWHERE + ' AND ORG.ORG_ID = ' + cast(@.ORG_ID as
> varchar(15))
> END
> IF @.PRG_Closed != 2 -- Add PRG_Closed to where
> IF @.intAND = 0
> BEGIN
> SET @.strWHERE = @.strWHERE + 'PRG.PRG_Closed = ' + cast(@.PRG_Closed
> as varchar(15))
> SET @.intAND = 1
> END
> ELSE
> BEGIN
> SET @.strWHERE = @.strWHERE + ' AND PRG.PRG_Closed = ' +
> cast(@.PRG_Closed as varchar(15))
> END
> IF @.strWHERE != 'WHERE ' -- if some parameters supplied..
> BEGIN -- Execute search
> EXECUTE (@.strSELECT + ' ' + @.strFROM + ' ' + @.strWHERE + ' ' +
> @.strORDERBY)
> RETURN(0)
> END
>
> /************ UPDATE COVENANT PRINT DATE ************************/
> -- Added by JSHAW 02/25/2004
> IF @.PRG_Covenant != 0 -- PRG_Covenant supplied
> BEGIN
> --SET @.ORD_Date = GETDATE()
> UPDATE PROGRAMS
> SET Covenant_Printed = convert(char(10),@.ORD_Date,101)
> WHERE PRG_ID = cast(@.PRG_ID as varchar(15))
> END
> GO