473,385 Members | 1,814 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,385 software developers and data experts.

User Defined function returning table give inconsistent results

Greetings

I have an ASP application to enter reservations. There are multiple user
security settings that require some users to have a restricted list of
client in a drop list on a form.

I constructed the following function:
CREATE FUNCTION [estudf_UserClientsList] (@pUserId int)
RETURNS @UserClientQ table
(
-- user information
UserId int,
FirstName varchar(50),
LastName varchar(50),
UserName varchar(25),
-- client information
ClientId int,
ClientName varchar(40),
UsageTypeId int,
-- event update rights
noSaveReq int, -- =1 disallow save of Request status events
for the client
noSaveConflict int, -- =1 disallow save of Conflict status
events for the client
noSaveApprov int, -- =1 disallow save of Approved status
events for the client
noSaveDenied int, -- =1 disallow save of Denied status events
for the client
-- debug trace
Stage int
)
AS
BEGIN
----------------------------------------------------------------------------
------------------------------
-- Marc Walgren - 07/14/2004
-- This routine will determine proper client list for user based on user
-- settings and user/contact/client relationships
----------------------------------------------------------------------------
------------------------------
-- local variables
declare @UserAdmin int
declare @UserFirstName varchar(50)
declare @UserLastName varchar(50)
declare @UserName varchar(25)
declare @UserActive int
declare @UserReadOnly int
declare @FacilityAdminCount int
declare @UserClientCount int

-- Users with Admin rights get all clients
select @UserAdmin = Admin,
@UserFirstName = FirstName,
@UserLastName = LastName,
@UserName = UserName,
@UserActive = Active,
@UserReadOnly = ReadOnly
from Users where UserId = @pUserId

-- clear return q to be sure
delete @UserClientQ

if @UserAdmin = 1
begin

-- give all clients
insert @UserClientQ ( UserId, FirstName, LastName, UserName,
ClientId, ClientName, UsageTypeId, Stage, noSaveReq, noSaveConflict,
noSaveApprov, noSaveDenied )
Select @pUserId, @UserFirstName, @UserLastName, @UserName,
c.ClientId, c.ClientName, c.UsageTypeId, 1, 0, 0, 0, 0
from Client c

end
else
begin
-- check for Building Administrator
select @FacilityAdminCount = count(UFLID) from UserFacilityLink
where UserId = @pUserId

if @FacilityAdminCount > 0
begin
-- give all clients
insert @UserClientQ ( UserId, FirstName, LastName, UserName,
ClientId, ClientName, UsageTypeId, Stage, noSaveReq, noSaveConflict,
noSaveApprov, noSaveDenied )
Select @pUserId, @UserFirstName, @UserLastName,
@UserName, c.ClientId, c.ClientName, c.UsageTypeId, 2, 0, 0, 0, 0
from Client c
end
else
begin
-- count the user's client
SELECT @UserClientCount = count(dbo.Users.UserId)
FROM dbo.Users
INNER JOIN dbo.Contacts ON dbo.Contacts.UserId =
dbo.Users.UserId
INNER JOIN dbo.ClientContactLink ON dbo.Contacts.ContactID =
dbo.ClientContactLink.ContactID
INNER JOIN dbo.Client ON dbo.Client.ClientID =
dbo.ClientContactLink.ClientID
WHERE dbo.Users.UserId = @pUserId

-- users with associated client get just their clients
if @UserClientCount > 0
begin
-- give user's clients
insert @UserClientQ ( UserId, FirstName, LastName,
UserName, ClientId, ClientName, UsageTypeId, Stage, noSaveReq,
noSaveConflict, noSaveApprov, noSaveDenied )
SELECT dbo.Users.UserId,
dbo.Users.FirstName, dbo.Users.LastName, dbo.Users.UserName,
dbo.Client.ClientId,
dbo.Client.ClientName, dbo.Client.UsageTypeId , 3, 0, 0, 1,1
FROM dbo.Users
INNER JOIN dbo.Contacts ON
dbo.Contacts.UserId = dbo.Users.UserId
INNER JOIN dbo.ClientContactLink ON
dbo.Contacts.ContactID = dbo.ClientContactLink.ContactID
INNER JOIN dbo.Client ON dbo.Client.ClientID
= dbo.ClientContactLink.ClientID
WHERE dbo.Users.UserId = @pUserId
end
else
begin
-- Active and non-readonly users get full client
list
if @UserActive = 1 and @UserReadOnly = 0
begin
-- give all clients
insert @UserClientQ ( UserId, FirstName,
LastName, UserName, ClientId, ClientName, UsageTypeId, Stage, noSaveReq,
noSaveConflict, noSaveApprov, noSaveDenied )
Select @pUserId, @UserFirstName,
@UserLastName, @UserName, c.ClientId, c.ClientName, c.UsageTypeId, 4, 0, 0,
1, 1
from Client c
end
end
end
end

return

END

My administrative user is ID=1. I perform the following code to build the
drop list
strSQL = "SELECT ClientID, ClientName, UsageTypeId, Stage FROM
estudf_UserClientsList(" strSQL = strSQL & cstr(iUserId)
strSQL = strSQL & ") ORDER BY ClientName"

response.write "<select size=1 name=txtClient>"
response.write "<option value='' >--Choose--</option>"

pbIsClientListExists = false
set rs=Server.CreateObject("ADODB.RecordSet")
rs.open strSQL,CONNECTIONSTRING

while rs.EOF=false
pbIsClientListExists = true
stage = rs.fields("Stage")
If Trim(rs.fields("ClientID"))=Trim(checkvalue)Then
selected=" selected"
pClientsUsageTypeID = rs.fields("UsageTypeId")
Else
selected=""
End If

response.write "<option value='"
response.write Trim(rs.fields("ClientID"))
response.write "' " & selected & ">"
response.write rs.fields("ClientName")
response.write "</option>"

wend
I have used Profiler to be sure the parameter to the function is proper and
have never found it to be incorrect.

When the client list is not build properly, only one client name appears and
it is clientid=1. If I change the select sql in the function to exclude
clientid=1 then the name of the next lowest client id is displayed when the
list is improperly built.

Any ideas to resolve the inconsistency would be gratefully received.

Marc
Jul 19 '05 #1
3 2056
There's too much for me to digest, but maybe one of the people over at
m.p.sqlserver.programming will take a crack at it ...

They'll want you to post table-creation DDL and insert statements with
sample data before they'll look at it.

Bob Barrows
Marc Walgren wrote:
Greetings

I have an ASP application to enter reservations. There are multiple
user security settings that require some users to have a restricted
list of client in a drop list on a form.

I constructed the following function:

<snip>

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #2
"Marc Walgren" <marcmittenATyahoo.com> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
[snip]
My administrative user is ID=1. I perform the following code to build the
drop list
strSQL = "SELECT ClientID, ClientName, UsageTypeId, Stage FROM
estudf_UserClientsList(" strSQL = strSQL & cstr(iUserId)
strSQL = strSQL & ") ORDER BY ClientName"

response.write "<select size=1 name=txtClient>"
response.write "<option value='' >--Choose--</option>"

pbIsClientListExists = false
set rs=Server.CreateObject("ADODB.RecordSet")
rs.open strSQL,CONNECTIONSTRING

while rs.EOF=false
pbIsClientListExists = true
stage = rs.fields("Stage")
If Trim(rs.fields("ClientID"))=Trim(checkvalue)Then
selected=" selected"
pClientsUsageTypeID = rs.fields("UsageTypeId")
Else
selected=""
End If

response.write "<option value='"
response.write Trim(rs.fields("ClientID"))
response.write "' " & selected & ">"
response.write rs.fields("ClientName")
response.write "</option>"

wend
I have used Profiler to be sure the parameter to the function is proper and have never found it to be incorrect.

When the client list is not build properly, only one client name appears and it is clientid=1. If I change the select sql in the function to exclude
clientid=1 then the name of the next lowest client id is displayed when the list is improperly built.

Any ideas to resolve the inconsistency would be gratefully received.


I don't see an "rs.MoveNext" call in your code. Also, you should really
consider rewriting that UDF. It could be done in a stored procedure with one
SELECT statement and a well placed CASE expression. Finally, you may also
want to consider using GetRows/GetString instead of recordset iteration.
Here's an article:

http://aspfaq.com/show.asp?id=2467
Jul 19 '05 #3
Chris

Thanks for you thoughts.

I omitted the movnext from my code snip for the post. I switch from the
getrow() approach as a wild attempt to resolve the instability.

The article you reference is good and I had already read it.

Best regards,

Marc

"Chris Hohmann" <no****@thankyou.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Marc Walgren" <marcmittenATyahoo.com> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
[snip]
My administrative user is ID=1. I perform the following code to build the drop list
strSQL = "SELECT ClientID, ClientName, UsageTypeId, Stage FROM
estudf_UserClientsList(" strSQL = strSQL & cstr(iUserId)
strSQL = strSQL & ") ORDER BY ClientName"

response.write "<select size=1 name=txtClient>"
response.write "<option value='' >--Choose--</option>"

pbIsClientListExists = false
set rs=Server.CreateObject("ADODB.RecordSet")
rs.open strSQL,CONNECTIONSTRING

while rs.EOF=false
pbIsClientListExists = true
stage = rs.fields("Stage")
If Trim(rs.fields("ClientID"))=Trim(checkvalue)Then
selected=" selected"
pClientsUsageTypeID = rs.fields("UsageTypeId")
Else
selected=""
End If

response.write "<option value='"
response.write Trim(rs.fields("ClientID"))
response.write "' " & selected & ">"
response.write rs.fields("ClientName")
response.write "</option>"

wend
I have used Profiler to be sure the parameter to the function is proper and
have never found it to be incorrect.

When the client list is not build properly, only one client name appears

and
it is clientid=1. If I change the select sql in the function to exclude
clientid=1 then the name of the next lowest client id is displayed when

the
list is improperly built.

Any ideas to resolve the inconsistency would be gratefully received.


I don't see an "rs.MoveNext" call in your code. Also, you should really
consider rewriting that UDF. It could be done in a stored procedure with

one SELECT statement and a well placed CASE expression. Finally, you may also
want to consider using GetRows/GetString instead of recordset iteration.
Here's an article:

http://aspfaq.com/show.asp?id=2467

Jul 19 '05 #4

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

Similar topics

9
by: Rowland Hills | last post by:
I have a table which is returning inconsistent results when I query it! In query analyzer: If I do "SELECT * FROM TABLE_NAME" I get no rows returned. If I do "SELECT COL1, COL2 FROM...
26
by: JGH | last post by:
How can I check if a key is defined in an associative array? var users = new array(); users = "Joe Blow"; users = "John Doe"; users = "Jane Doe"; function isUser (userID) { if (?????)
6
by: Ken Post | last post by:
Hi All: I've read a whole slew of posts about creating temp tables using stored proceedures to get the crosstab ability, but I'm wondering if, for this specific case, there might be a more...
4
by: Sameer Deshpande | last post by:
How do I create and return user defined data types in DB2. F.ex In Oracle I can create a user define datatype and return this data type from stored function. How can I do the same in DB2? ...
3
by: chreo | last post by:
I have user-defined function in MSSQL which returns Table (with 10 columns) (sorry for Polish names) CREATE FUNCTION PACZKI_Z_AKCJI (@AKCJA_ID int) RETURNS TABLE RETURN SELECT TOP 100...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
4
by: shark | last post by:
hi, it might be a very stupid question but i want to know whether an execution plan is created in user defined functions in sql server 2000 like stored procedures. Thanks.
0
by: cuddles | last post by:
hi all i need to declare a temporary table within a user-defined function in IBM DB2 ver 8. can someone give me the syntax for the temp table declaration. CREATE FUNCTION PEGASUS.readOrder(...
2
by: emmettnicholas | last post by:
Hi, I realize that eval() is generally discouraged, but I've found myself wishing that I could execute user-generated code. One idea I've seen is to use token_get_all(), and then make sure no...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.