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 |
|
t1g312
Posting Yak Master
148 Posts |
Posted - 2004-06-26 : 08:09:24
|
| Hi all,How do I write a SP that returns columns to the client application? ThanksAdi-------------------------/me sux @sql server |
|
|
Kristen
Test
22859 Posts |
Posted - 2004-06-26 : 10:37:51
|
Either return them as a result set ("SELECT MyColumn1, MyColumn2 ...") or as output variables:CREATE PROCEDURE MySProc MyParameter1 int, MyParameter2 varchar(10), MyOutParameter3 int OUTPUT, MyOutParameters4 varchar(10) OUTPUT NULLAS... Call withEXEC MySProc 123, 'ABC', @MyIntResult3 OUTPUT, @MyStrResult4 OUTPUTthe last is optional ...If you are calling from, say, VBScript/ASP there are commands to "prepare" variables for the output parameters.OUTPUT parameters do NOT have to be provided as parmaeters - you can pass a value and thus ignore any output value.Kristen |
 |
|
|
t1g312
Posting Yak Master
148 Posts |
Posted - 2004-06-27 : 00:30:58
|
| Hi Kristen,Thanks for that. I am calling from a VB program. I am currently using dynamic sql statements and want to move to SPs. I know there's no escaping from learning T-Sql but I just want to have a feel of SPs before I go full steam ahead. Is there any website that has samples of VB code and SPs?Adi-------------------------/me sux @sql server |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2004-06-27 : 06:09:21
|
Yes I'm sure there are, but I can't say I'm familiar with them - we wrote our VBscript "Call an SProc Function" years ago, and I'm not sure what resources we used - either way there will be much better ones available now.I'll just pull a few lines out of our stuff to give you some things to search on. In isolation these may not work, but hopefully it gives you some ideas: Set oCMD = Server.CreateObject("ADODB.Command") Set oRS = Server.CreateObject("ADODB.RecordSet") oCMD.ActiveConnection = MyADOConenction oCMD.CommandType = 4 ' adCmdStoredProc oCMD.CommandText = "" oCMD.CommandTimeout = 30' Refresh parameters from DB: oCmd.Parameters.Refresh' Parameter name = oCmd.Parameters(nItem).Name ' Set parameter (note: data type needs to be accomodated): oCmd.Parameters(nItem).value = MyParameterValue oRS.CursorType = 0 ' Can't remember what the name of this is oRS.CursorLocation = 3 ' Can't remember what the name of this is set oRS.Source = oCMD oRS.open ' Execute it!' Return value: oCmd.Parameters(0).value If MyADOConenction.errors.count > 0 then' Process ADO errors for each objerror in MyADOConenction.errors m_strError = m_strError _ & (objerror.description & chr(13) & chr(10)) next End If set oCMD=nothingYour SProcs need to be something like:CREATE PROCEDURE xsp_MySProc -- do NOT start name with "sp_..." @myParameter1 int, @myParameter2 varchar(10), @myParameter3 int=5 -- OptionalASSET NOCOUNT ONSET XACT_ABORT ON -- Abort on errorDECLARE @MyLocalVariable1 int, @MyLocalVariable varchar(10)SELECT MyCol1, MyCol2FROM dbo.MyTableWHERE MyPKColum1 = @myParameter1 AND MyPKColum2 = @myParameter2ORDER BY MyCol1, MyCol2SELECT @intErrNo = @@ERROR, @intRowCount = @@ROWCOUNTIF @intErrNo <> 0 -- OR @intRowCount < 1 -- include if NO rows is an error ....BEGIN -- Error Handler RAISERROR(....) GOTO MySProc_EXITEND...MySProc_EXIT:SET NOCOUNT OFF RETURNGO Kristen |
 |
|
|
t1g312
Posting Yak Master
148 Posts |
Posted - 2004-06-27 : 07:46:42
|
| Hi Kristen,Thanks a lot for that! BTW, why do you say "do not start the name with sp_"?Adi-------------------------/me sux @sql server |
 |
|
|
t1g312
Posting Yak Master
148 Posts |
Posted - 2004-06-27 : 07:57:23
|
| A couple of problems:1. Setting recordsource source to command doesn't seem to work.2. In your SP, you used "SELECT MyCol1, MyCol2...". Don't you have to create output parameters for each of the columns and assign each value to that i.e. "SELECT @MyCol1 = MyCol1..."ThanksAdi-------------------------/me sux @sql server |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2004-06-27 : 12:20:50
|
quote: Originally posted by t1g312 BTW, why do you say "do not start the name with sp_"?
Because SQL till look in MASTER first for an SProc of that name. Even if there isn't one it makes for poor performance because SQL wil always "check" MASTER for an SProc of that nameKristen |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2004-06-27 : 12:26:38
|
| >> 1. Setting recordsource source to command doesn't seem to work.There's a fair amount of other wibble in our functions to get all the framework in place, sorry.>> 2. In your SP, you used "SELECT MyCol1, MyCol2...". Don't you have to create output parameters for each of the columns and assign each value to that i.e. "SELECT @MyCol1 = MyCol1..."Well you could do, but @MyCol1 is only going to return the value from ONE row - which may be what you want, but.If you are returning results to another SProc then a SELECT MyCol1, MyCol2 ... is not going to be a lot of use (the caller could store the results into a temporary table, but realistically that's unlikely to be useful in the majority of cases).However, if you are returning results to an application a resultset is often Just Fine. For example, the Invoice History for a given client.The Client Application can LOOP around the results and process them, whereas SQL is much better off processing the results as a "set" of data, rather than one-by-one.So my general rule-of-thumb is to expect toreturn a RecordSet to an application and some OUTPUT parameters to another SProcKristen |
 |
|
|
t1g312
Posting Yak Master
148 Posts |
Posted - 2004-06-28 : 00:34:01
|
| Thanks again! Really appreciate it!Adi-------------------------/me sux @sql server |
 |
|
|
|
|
|
|
|