473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Stored Procedures

Hi all

Im relatively new to using stored procedures and im not sure if it is
possible to do what I am trying to do so any help here is greatly
appreciated. I am using the variable @MachineName which is obviously the
local machine name mainly in this procedure. What is loop through from the
first character of the variable to the last and use this data in a select
statement. I have included the code below for what I have tried so far but I
get an error that "Error 116: Only one expression can be specified in the
list when the subquery is not introduced with EXISTS". So im not sure if
im going about this the right way or not but any help anyone give is greatly
appreciated

Thanks
/*
** Determine Entity Group Memberships
*/

CREATE PROCEDURE [dbo].[sp_EntityStartup]

@MachineName VarChar(50),
@UserName VarChar(50)

AS

DECLARE @MachineLength Char(1) /* Local Machine Name Length */
DECLARE @MachInt Char(1) /* Machine Integer Counter */

SET @MachInt = 1

SELECT @MachineLength = Len(@MachineName)
DECLARE @Ttp VarChar(20)

WHILE @MachInt <= @MachineLength

BEGIN

SELECT @Ttp = (SELECT * FROM GrpMachines WHERE MachineGrp LIKE
LEFT(@MachineName,@MachInt))
SELECT @MachInt = @MachInt + 1

END

GO
Jul 20 '05 #1
3 6554

"Jarrod Morrison" <ja*****@ihug.com.au> wrote in message
news:bm**********@lust.ihug.co.nz...
Hi all

Im relatively new to using stored procedures and im not sure if it is
possible to do what I am trying to do so any help here is greatly
appreciated. I am using the variable @MachineName which is obviously the
local machine name mainly in this procedure. What is loop through from the
first character of the variable to the last and use this data in a select
statement. I have included the code below for what I have tried so far but I get an error that "Error 116: Only one expression can be specified in the
list when the subquery is not introduced with EXISTS". So im not sure if
im going about this the right way or not but any help anyone give is greatly appreciated

Thanks
/*
** Determine Entity Group Memberships
*/

CREATE PROCEDURE [dbo].[sp_EntityStartup]

@MachineName VarChar(50),
@UserName VarChar(50)

AS

DECLARE @MachineLength Char(1) /* Local Machine Name Length */
DECLARE @MachInt Char(1) /* Machine Integer Counter */

SET @MachInt = 1

SELECT @MachineLength = Len(@MachineName)
DECLARE @Ttp VarChar(20)

WHILE @MachInt <= @MachineLength

BEGIN

SELECT @Ttp = (SELECT * FROM GrpMachines WHERE MachineGrp LIKE
LEFT(@MachineName,@MachInt))
SELECT @MachInt = @MachInt + 1

END

GO


The error message is because you have a scalar variable @Ttp, but your
subquery can return multiple rows and columns, so the result of the subquery
cannot be assigned to it. Perhaps you meant to do something like this?

select @Ttp = count(*)
from dbo.GrpMachines
where ...

In addition, you don't do anything with @Ttp inside the loop - it's simply
updated on each pass though - and the procedure doesn't return any output,
so it's not clear what you're trying to achieve. Finally, it's generally
best to avoid naming stored procedures as sp_ - that's used for system
stored procedures. This may be more like what you want, although I'm just
guessing:
CREATE PROCEDURE dbo.GetEntityStartup
@MachineName VarChar(50)
AS
begin

/* Table to hold output */
create table #output (PartialName varchar(50), Matches int)

/* Loop Counter */
declare @i int
set @i = 1

/* Populate working table */
while @i <= len(@MachineName)
begin
insert into #output (PartialName, Matches)
select left(@MachineName, @i), count(*)
from GrpMachines
where MachineGrp like left(@MachineName, @i) + '%'

set @i = @i + 1
end

/* Return output */
select PartialName, Matches
from #output
order by PartialName

end
Simon
Jul 20 '05 #2
Hey Simon

Thanks for your help and advice with the stored procedures. I have changed
the code u gave me a little to do what i needed. The procedure does what i
need ok but i was wondering how to output this data. I have other stored
procedures where i specify that the variable is for output but im not sure
what type of variable to be using (IE im using VarChar at the moment, is
there an array type ?). At the moment i get the grid showing the results of
the query in sql query analyzer but i would like to output it somehow so
that when i am using the stored procedure with my external program i can use
the data from this stored procedure.

Thanks again for all your help
/*
** Determine Entity Group Memberships
*/

CREATE PROCEDURE [dbo].[EntityStartup]

@MachineName VarChar(50),
@UserName VarChar(50),

AS

DECLARE @MachineLength Char(2) /* Local Machine Name Length */
DECLARE @MachInt Char(1) /* Machine Integer Counter */

SET @MachInt = 1

SELECT @MachineLength = Len(@MachineName)

CREATE TABLE #GrpMem (GrpName varchar(50), AuthID varchar(50))

WHILE @MachInt <= @MachineLength

BEGIN
INSERT INTO #GrpMem (GrpName, AuthID) SELECT * FROM GrpMachines WHERE
MachineGrp LIKE LEFT(@MachineName,@MachInt)
SET @MachInt = @MachInt + 1
END

SELECT * FROM #GrpMem

GO

"Simon Hayes" <sq*@hayes.ch> wrote in message
news:3f**********@news.bluewin.ch...

"Jarrod Morrison" <ja*****@ihug.com.au> wrote in message
news:bm**********@lust.ihug.co.nz...
Hi all

Im relatively new to using stored procedures and im not sure if it is
possible to do what I am trying to do so any help here is greatly
appreciated. I am using the variable @MachineName which is obviously the
local machine name mainly in this procedure. What is loop through from the first character of the variable to the last and use this data in a select statement. I have included the code below for what I have tried so far but
I
get an error that "Error 116: Only one expression can be specified in
the list when the subquery is not introduced with EXISTS". So im not sure if im going about this the right way or not but any help anyone give is

greatly
appreciated

Thanks
/*
** Determine Entity Group Memberships
*/

CREATE PROCEDURE [dbo].[sp_EntityStartup]

@MachineName VarChar(50),
@UserName VarChar(50)

AS

DECLARE @MachineLength Char(1) /* Local Machine Name Length */
DECLARE @MachInt Char(1) /* Machine Integer Counter */

SET @MachInt = 1

SELECT @MachineLength = Len(@MachineName)
DECLARE @Ttp VarChar(20)

WHILE @MachInt <= @MachineLength

BEGIN

SELECT @Ttp = (SELECT * FROM GrpMachines WHERE MachineGrp LIKE
LEFT(@MachineName,@MachInt))
SELECT @MachInt = @MachInt + 1

END

GO


The error message is because you have a scalar variable @Ttp, but your
subquery can return multiple rows and columns, so the result of the

subquery cannot be assigned to it. Perhaps you meant to do something like this?

select @Ttp = count(*)
from dbo.GrpMachines
where ...

In addition, you don't do anything with @Ttp inside the loop - it's simply
updated on each pass though - and the procedure doesn't return any output,
so it's not clear what you're trying to achieve. Finally, it's generally
best to avoid naming stored procedures as sp_ - that's used for system
stored procedures. This may be more like what you want, although I'm just
guessing:
CREATE PROCEDURE dbo.GetEntityStartup
@MachineName VarChar(50)
AS
begin

/* Table to hold output */
create table #output (PartialName varchar(50), Matches int)

/* Loop Counter */
declare @i int
set @i = 1

/* Populate working table */
while @i <= len(@MachineName)
begin
insert into #output (PartialName, Matches)
select left(@MachineName, @i), count(*)
from GrpMachines
where MachineGrp like left(@MachineName, @i) + '%'

set @i = @i + 1
end

/* Return output */
select PartialName, Matches
from #output
order by PartialName

end
Simon

Jul 20 '05 #3

"Jarrod Morrison" <ja*****@ihug.com.au> wrote in message
news:bn**********@lust.ihug.co.nz...
Hey Simon

Thanks for your help and advice with the stored procedures. I have changed
the code u gave me a little to do what i needed. The procedure does what i
need ok but i was wondering how to output this data. I have other stored
procedures where i specify that the variable is for output but im not sure
what type of variable to be using (IE im using VarChar at the moment, is
there an array type ?). At the moment i get the grid showing the results of the query in sql query analyzer but i would like to output it somehow so
that when i am using the stored procedure with my external program i can use the data from this stored procedure.

Thanks again for all your help
/*
** Determine Entity Group Memberships
*/

CREATE PROCEDURE [dbo].[EntityStartup]

@MachineName VarChar(50),
@UserName VarChar(50),

AS

DECLARE @MachineLength Char(2) /* Local Machine Name Length */
DECLARE @MachInt Char(1) /* Machine Integer Counter */

SET @MachInt = 1

SELECT @MachineLength = Len(@MachineName)

CREATE TABLE #GrpMem (GrpName varchar(50), AuthID varchar(50))

WHILE @MachInt <= @MachineLength

BEGIN
INSERT INTO #GrpMem (GrpName, AuthID) SELECT * FROM GrpMachines WHERE
MachineGrp LIKE LEFT(@MachineName,@MachInt)
SET @MachInt = @MachInt + 1
END

SELECT * FROM #GrpMem

GO


<snip>

That all depends how your application calls the procedure. If you're using
ADO, for example, you'd get the result set (ie the rows you see in QA) into
a recordset object, and do something with it there. Other client libraries
(ODBC, JDBC etc.) will have their own way of handling it.

You can't use a table variable as an output parameter from a stored proc, so
if you need to pass the result set to another stored proc, then you could
look at using a table-valued UDF instead, as discussed here (among other
approaches):

http://www.algonet.se/~sommar/share_data.html
Simon
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: jrefactors | last post by:
I want to know the differences between SQL Server 2000 stored procedures and oracle stored procedures? Do they have different syntax? The concept should be the same that the stored procedures...
2
by: scott | last post by:
Hi, Just wondering what sort of problems and advantages people have found using stored procedures. I have an app developed in VB6 & VB.NET and our developers are starting to re-write some of the...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
5
by: Rhino | last post by:
I am trying to determine the behaviour of stored procedures in DB2 V8.2.x in Windows/Unix/Linux and how I can control that behaviour. Some documentation in the manuals is confusing the issue...
5
by: Tim Marshall | last post by:
I was following the thread "Re: Access Treeview - Is it Safe Yet?" with interest and on reading the post describing Lauren Quantrell's SmartTree, I've run into something I don't understand: Stored...
2
by: Eli | last post by:
Hi all We currently have a strange problem with calling a Stored Procedure (SQL Database) in our C# Project. The only error I get is "System error" which says a lot :) Background: We have...
0
by: Amber | last post by:
Stored procedures are faster and more efficient than in-line SQL statements. In this article we will look at two SQL Server stored procedures; one using an input parameter and one not, and see how...
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
11
by: peter | last post by:
I am trying to get a SQL stored procedure to use user maintained MQT implicitly which raises questions on when they are used or not used. In theory you would expect the stored procedure to pick up...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.