Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-02-13 : 03:04:16
|
I have a query that uses variables and returns a normal resultset. I need to use this query in several programs. It has two parameters (which are currently set in the code likeSET @user = 'myname'; --User nameSET @application = '1'; --Application).I would want it to be something way simpler for the programs to use than copying the 200 rows of code to there. I'm looking for something likeSet ResultSet = LongQueryProgram(user, 1)so what do I need on the SQL Server end? A stored procedure? How do I create that for this kind of case?When I right click on Stored Procedure folder of my database and take "new stored procedure", it gives:CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> -- Add the parameters for the stored procedure here <@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, <@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>ENDGOHow do I go from here? What's a sysname? What's the difference between Procedure_Name and ProcedureName? Do I need greater than and less than characters in the syntax? |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-02-13 : 04:52:35
|
Reading your requirement, I think what you're after is a stored procedureThe code above is just a stub for creating a procedure. The <> denotes place holders.In your case it would beCREATE PROC PutYourNameHere@User varchar(30),@application intAS...your code hereGO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-02-13 : 05:39:49
|
quote: Originally posted by visakh16 Reading your requirement, I think what you're after is a stored procedureThe code above is just a stub for creating a procedure. The <> denotes place holders.In your case it would beCREATE PROC PutYourNameHere@User varchar(30),@application intAS...your code hereGO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Thanks, I was able to create it!This is now quite close to work, I need to post this question in VBA-centered forum too, but just in case, could you spot the error here:Function LoadAuthorizationInfoFromSQL(iApp As Integer, strUser As String) As Variant strConn = ConnectToSQL() Set oConn = New ADODB.Connection Set rs = New ADODB.Recordset Set cmd = New ADODB.Command oConn.Open strConn stProcName = "GetUserAuthForApp" cmd.CommandType = adCmdStoredProc cmd.ActiveConnection = oConn cmd.CommandText = stProcName Set prm = cmd.CreateParameter("@User", adVarChar, adParamInput, 7) cmd.Parameters.Append prm Set prm = cmd.CreateParameter("@application", adInteger, adParamInput) cmd.Parameters.Append prm rs.Open cmd.Execute...and so on...The error comes at that rs.Open cmd.Executeit saysProcedure of function 'GetUserAuthForApp' expects parameter '@User', which was not suppliedThe beginning of the stored procedure looks now like this:CREATE PROC GetUserAuthForApp@User varchar(7),@application intAS |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-02-13 : 05:48:59
|
quote: Originally posted by KilpAr
quote: Originally posted by visakh16 Reading your requirement, I think what you're after is a stored procedureThe code above is just a stub for creating a procedure. The <> denotes place holders.In your case it would beCREATE PROC PutYourNameHere@User varchar(30),@application intAS...your code hereGO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Thanks, I was able to create it!This is now quite close to work, I need to post this question in VBA-centered forum too, but just in case, could you spot the error here:Function LoadAuthorizationInfoFromSQL(iApp As Integer, strUser As String) As Variant strConn = ConnectToSQL() Set oConn = New ADODB.Connection Set rs = New ADODB.Recordset Set cmd = New ADODB.Command oConn.Open strConn stProcName = "GetUserAuthForApp" cmd.CommandType = adCmdStoredProc cmd.ActiveConnection = oConn cmd.CommandText = stProcName Set prm = cmd.CreateParameter("@User", adVarChar, adParamInput, 7) cmd.Parameters.Append prm Set prm = cmd.CreateParameter("@application", adInteger, adParamInput) cmd.Parameters.Append prm rs.Open cmd.Execute...and so on...The error comes at that rs.Open cmd.Executeit saysProcedure of function 'GetUserAuthForApp' expects parameter '@User', which was not suppliedThe beginning of the stored procedure looks now like this:CREATE PROC GetUserAuthForApp@User varchar(7),@application intAS
try as above without @ sign------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-02-13 : 07:11:33
|
One clear error there is that I don't use the values anywhere - I have parameters, but no values assigned to them!The code looks now like thisDim strConn As String 'Connection string to SQL ServerDim strSQLtoExecute As String 'SQL query string to executeDim oConn As ADODB.Connection 'Object for connectingDim rs As ADODB.Recordset 'Object for recordsetDim cmd As ADODB.CommandDim prmUser As ADODB.ParameterDim prmApplication As ADODB.ParameterDim stProcName As String 'Stored Procedure nameFunction LoadAuthorizationInfoFromSQL(iApp As Integer, strUser As String) As Variant 'Define connection string and open the connection strConn = ConnectToSQL() 'Initialize variables for database connections Set oConn = New ADODB.Connection Set rs = New ADODB.Recordset Set cmd = New ADODB.Command oConn.Open strConn stProcName = "GetUserAuthForApp" 'Define name of Stored Procedure to execute. cmd.CommandType = adCmdStoredProc 'Define the ADODB command cmd.ActiveConnection = oConn 'Set the command connection string cmd.CommandText = stProcName 'Define Stored Procedure to run Set prmUser = cmd.CreateParameter("User", adVarChar, adParamInput, 7) cmd.Parameters.Append prmUser Set prmApplication = cmd.CreateParameter("application", adInteger, adParamInput) cmd.Parameters.Append prmApplication prmUser.Value = "'" & strUser & "'" 'Tried also without quotes, no difference prmApplication.Value = iApp 'Execute stored procedure and return to a recordset rs.Open cmd.Execute and the error is now"Arguments are of wrong type, are out of acceptable range, or are in confilict with one another" |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-02-13 : 07:23:30
|
what are values you pass for iApp and strUser?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-02-13 : 07:42:52
|
The problem is now something in the stored_procedure, probably something in the way to return resultset. Where do I declare the return value in the stored procedure? When i create the procedure and run from management studio, it wants to return an int. How do I change that into a resultset? |
|
|
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-02-13 : 07:49:47
|
quote: Originally posted by visakh16 what are values you pass for iApp and strUser?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
iApp = 1strUser = kilar |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2013-02-13 : 08:08:43
|
put set nocount onat the top of the spthen put a select statement at the end to return the resultset.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-02-13 : 10:20:34
|
After quite a bit of fight, it finally works from SQL Server Management Studio - but not from VBAIn SQL Server Management Studio I can run it like this:USE [MyDatabase]GODECLARE @return_value int,@application int,@user varchar(7)EXEC@return_value = [dbo].[GetUserAuthForApp]@user = 'kilar', @application = 1GO From VBA, this doesn't work:Function LoadAuthorizationInfoFromSQL(iApp As Integer, strUser As String) As VariantDim oConn As ADODB.Connection 'Object for connectingDim rs As ADODB.Recordset 'Object for recordsetDim cmd As ADODB.CommandDim prmUser As ADODB.ParameterDim prmApplication As ADODB.ParameterDim stProcName As String 'Define connection string and open the connection strConn = ConnectToSQL() 'Initialize variables for database connections Set oConn = New ADODB.Connection Set rs = New ADODB.Recordset Set cmd = New ADODB.Command oConn.Open strConn stProcName = "[MyDatabase].[dbo].[GetUserAuthForApp]" cmd.CommandType = adCmdStoredProc 'Define the ADODB command cmd.ActiveConnection = oConn 'Set the command connection string cmd.CommandText = stProcName 'Define Stored Procedure to run Set prmUser = cmd.CreateParameter("@user", adVarChar, adParamInput, 7) prmUser.Value = "kilar" cmd.Parameters.Append prmUser Set prmApplication = cmd.CreateParameter("@application", adInteger, adParamInput) prmApplication.Value = 1 cmd.Parameters.Append prmApplication 'Execute stored procedure and return to a recordset rs.Open cmd.Execute'already crashed by this point The stored procedure looks like:USE [MyDatabase]SET NOCOUNT ONGOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROC [dbo].[GetUserAuthForApp]@user varchar(7),@application intASDECLARE @tempauthtable varchar(max);DECLARE @querystring varchar(max);SET @tempauthtable = '##UserAuthForApp_' + @userSET @querystring ='IF NOT EXISTS(SELECT * FROM tempdb.sys.objects WHERE name = ''' + @tempauthtable + ''') CREATE TABLE ' + @tempauthtable + 'tablestructurehere';EXEC (@querystring);--building a dynamic temp table here about 150 rows cut outEXEC (@querystring);SET @querystring = 'SELECT * FROM ' + @tempauthtable;EXEC (@querystring); Error is still:"Arguments are of wrong type, are out of acceptable range, or are in confilict with one another" |
|
|
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-02-14 : 09:50:45
|
Now the VBA part looks like this: Set rs = cmd.Execute rs.Open Range("F1").CopyFromRecordset rsand it complains the recordset is closed. So I do get the recordset, I just can't read anything from it. |
|
|
|
|
|
|
|