Showing posts with label username. Show all posts
Showing posts with label username. Show all posts

Monday, March 26, 2012

Help with SP to check and add new user to database

Hi - I'm new to SPs, and wondered if anyone could help with this SP.
It takes a username, email and password - and should register (add) the
details to the customer table. But first, I would like it to check if
the email address already exists in the DB - by performing the first
SELECT statement.
If it does exist, I would like it to just return 0 (and my code in the
.net page will interpret 0 as 'email already exists' and flag that to
the user.
Trouble is, it just keeps returning 0, and not adding the new details.
Thanks for any help,
Mark
CREATE Procedure CustomerAdd
(
@.FullName varchar(50),
@.Email varchar(50),
@.Password varchar(50),
@.CustomerID int OUTPUT
)
AS
SELECT CustomerID FROM Customers WHERE EmailAddress = @.Email
IF @.@.Rowcount >0
SELECT
@.CustomerID = 0
ELSE
INSERT INTO Customers
(
FullName,
EMailAddress,
Password
)
VALUES
(
@.FullName,
@.Email,
@.Password
)
SELECT
@.CustomerID = @.@.Identity
GO
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!Try,
CREATE Procedure CustomerAdd
(
@.FullName varchar(50),
@.Email varchar(50),
@.Password varchar(50),
@.CustomerID int OUTPUT
)
AS
set nocount on
declare @.error int
if exists(SELECT * FROM Customers WHERE EmailAddress = @.Email)
set @.CustomerID = 0
else
begin
INSERT INTO Customers
(
FullName,
EMailAddress,
Password
)
VALUES
(
@.FullName,
@.Email,
@.Password
)
set @.error = @.@.error
if @.error = 0
set @.CustomerID = scope_Identity()
else
return -1
end
return 0
GO
AMB
"Mark" wrote:

> Hi - I'm new to SPs, and wondered if anyone could help with this SP.
> It takes a username, email and password - and should register (add) the
> details to the customer table. But first, I would like it to check if
> the email address already exists in the DB - by performing the first
> SELECT statement.
> If it does exist, I would like it to just return 0 (and my code in the
> .net page will interpret 0 as 'email already exists' and flag that to
> the user.
> Trouble is, it just keeps returning 0, and not adding the new details.
> Thanks for any help,
> Mark
>
> CREATE Procedure CustomerAdd
> (
> @.FullName varchar(50),
> @.Email varchar(50),
> @.Password varchar(50),
> @.CustomerID int OUTPUT
> )
> AS
> SELECT CustomerID FROM Customers WHERE EmailAddress = @.Email
> IF @.@.Rowcount >0
> SELECT
> @.CustomerID = 0
> ELSE
> INSERT INTO Customers
> (
> FullName,
> EMailAddress,
> Password
> )
> VALUES
> (
> @.FullName,
> @.Email,
> @.Password
> )
> SELECT
> @.CustomerID = @.@.Identity
> GO
>
>
> *** Sent via Developersdex http://www.examnotes.net ***
> Don't just participate in USENET...get rewarded for it!
>|||Something like this?
CREATE Procedure CustomerAdd
(
@.FullName varchar(50),
@.Email varchar(50),
@.Password varchar(50),
@.CustomerID int OUTPUT
)
AS
IF NOT EXISTS (SELECT * FROM Customers WHERE EmailAddress = @.Email)
BEGIN
INSERT INTO Customers
(
FullName,
EMailAddress,
Password
)
VALUES
(
@.FullName,
@.Email,
@.Password
)
SELECT @.CustomerID = @.@.Identity
RETURN (0)
END
SET @.customerID = 0
/*
you could also do something like this:
SELECT @.CustomerID = CustomerID FROM Customers WHERE EmailAddress = @.Email
*/
RETURN (0)
GO
Keith
"Mark" <anonymous@.devdex.com> wrote in message
news:eg1sIT8AFHA.1084@.tk2msftngp13.phx.gbl...
> Hi - I'm new to SPs, and wondered if anyone could help with this SP.
> It takes a username, email and password - and should register (add) the
> details to the customer table. But first, I would like it to check if
> the email address already exists in the DB - by performing the first
> SELECT statement.
> If it does exist, I would like it to just return 0 (and my code in the
> net page will interpret 0 as 'email already exists' and flag that to
> the user.
> Trouble is, it just keeps returning 0, and not adding the new details.
> Thanks for any help,
> Mark
>
> CREATE Procedure CustomerAdd
> (
> @.FullName varchar(50),
> @.Email varchar(50),
> @.Password varchar(50),
> @.CustomerID int OUTPUT
> )
> AS
> SELECT CustomerID FROM Customers WHERE EmailAddress = @.Email
> IF @.@.Rowcount >0
> SELECT
> @.CustomerID = 0
> ELSE
> INSERT INTO Customers
> (
> FullName,
> EMailAddress,
> Password
> )
> VALUES
> (
> @.FullName,
> @.Email,
> @.Password
> )
> SELECT
> @.CustomerID = @.@.Identity
> GO
>
>
> *** Sent via Developersdex http://www.examnotes.net ***
> Don't just participate in USENET...get rewarded for it!|||On Wed, 26 Jan 2005 08:33:34 -0800, Mark wrote:

>Hi - I'm new to SPs, and wondered if anyone could help with this SP.
>It takes a username, email and password - and should register (add) the
>details to the customer table. But first, I would like it to check if
>the email address already exists in the DB - by performing the first
>SELECT statement.
(snip)
Hi Mark,
No need to do the check in a seperate operation:
CREATE Procedure CustomerAdd
(
@.FullName varchar(50),
@.Email varchar(50),
@.Password varchar(50),
@.CustomerID int OUTPUT
)
AS
INSERT INTO Customers
(
FullName,
EMailAddress,
Password
)
SELECT
@.FullName,
@.Email,
@.Password
WHERE NOT EXISTS
(SELECT *
FROM Customers
WHERE EmailAddress = @.Email)
IF @.@.Rowcount >0
SET @.CustomerID = 0
ELSE
SET @.CustomerID = SCOPE_IDENTITY
(untested)
Another alternative is to create a UNIQUE constraint on the EmailAddress
column and check for errors after the attempted insert (though I
personally prefer to prevent constraint violations).
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)

Friday, February 24, 2012

help with limiting query results

Hi,

I have the following table in postgres:

hostname | username | logontime
----+-----+---------
ws1 | rautaonn | 2004-01-13 21:25:01.100336
ws1 | administrator | 2004-01-13 21:25:07.706546
ws1 | testuser | 2004-01-13 21:25:16.084844
ws2 | testuser | 2004-01-13 21:25:18.683653
ws2 | testuser2 | 2004-01-13 21:25:20.862199
ws2 | administrator | 2004-01-13 21:25:25.932736
ws2 | oizone | 2004-01-13 21:25:30.107574

and I would need to create a query that selects each hostname only once with username that has the latest timestamp in the logontime column.
The real table has about 5000 rows with 500 different hostnames, and I would need this query for reporting. Any help would be appreciated.

Thank in advance.

-Onni Rautanenselect t.*
from table t
where logontime = (select max(logontime) from table where id = t.id);