Monday, March 19, 2012
Is Stored Procedure and 'in' broken in MSDE 2000/SQL 2000 SP4?
fine, basically 'in' using statored procedures seems to be performing as 'in'
or 'is' where as sending a query direct (identical) only performs 'in' as
expected
Example
Use msdb
Select * From Sysalerts Where database_name in (Select
convert(sysname(128),null))
returns nothing
but put the select command in a stored proceedure and it will return all the
rows with database_name that is null
Am i not seeing/setting something?
Simon
On Tue, 21 Jun 2005 08:36:05 -0700, "Tuner Fich" <Tuner
Fich@.discussions.microsoft.com> wrote:
>This is not the command that im running but demonstrates the problem just
>fine, basically 'in' using statored procedures seems to be performing as 'in'
>or 'is' where as sending a query direct (identical) only performs 'in' as
>expected
>Example
>Use msdb
>Select * From Sysalerts Where database_name in (Select
>convert(sysname(128),null))
>returns nothing
>but put the select command in a stored proceedure and it will return all the
>rows with database_name that is null
>Am i not seeing/setting something?
>Simon
Hi Simon,
I guess that you normally have the setting SET ANSI_NULLS ON (which is
fine, as it makes SQL Server treat NULLS as defined in the ANSI
standard, making your code more portable). However, when creating stored
procedure, someow the setting gets changed to SET ANSI_NULLS OFF (which
is definitely NOT fine, as it makes SQL Server treat NULLS in a
non-standard way that might appear logical at first glance but is not,
and that will make other database programmers fail to understand your
code).
With the ANSI standard ebhaviour for NULLS, logical expressions use
three-valued logic (True, False and Unknown) and all comparisons to NULL
will always return Unknown. The only valid way to copmpare a column or
variable to NULL is to use "WHERE column IS [NOT] NULL".
Also, stop using [NOT] IN with a subselect, as they are a source of
confusion with ANSI standard settings, and they can always be
transformed into a [NOT] EXISTS subquery that usually performs better as
well.
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
Monday, March 12, 2012
Is SQL Server 2005 Encryption FIPS-140 Validated ?
y".
Have these modules been certified and validated to FIPS-140 ?
Where can I find supporting Documentation or Certificate Number ?
Thank you (in advance)SQL Server 2005 uses certified Windows Crypto modules. For details, see
http://www.microsoft.com/technet/ar...fr=
true
This posting is provided "AS IS" with no warranties, and confers no rights.
HTH,
~ Remus Rusanu
SQL Service Broker
http://msdn2.microsoft.com/en-us/library/ms166043(en-US,SQL.90).aspx
"ITContractor" <ITContractor@.discussions.microsoft.com> wrote in message
news:16B7B4F6-04A9-42B5-B59A-83D91D51D1C1@.microsoft.com...
> SQL Server 2005 ships with (extended) Stored Procedures such as
> "EncryptByKey".
> Have these modules been certified and validated to FIPS-140 ?
> Where can I find supporting Documentation or Certificate Number ?
> Thank you (in advance)
Monday, February 20, 2012
Is my SProc written correctly?
I am very new to SQL server and I'm using stored procedures for my program. So far I wrote one tonight, that works just fine.
I haven't really written one before, but its kind of similar syntax since I know C++/C# and VB.
My question is, even though this works for what I need it do, is it written correctly? Can you see any problems with it, or would you have done it differently?
I want to make sure its done correctly, and it runs as fast as possible.
Thanks!
[pre]
CREATE PROCEDURE CreateNewUser
@.UserID int out,
@.LoginID nvarchar(30),
@.Password nvarchar(30),
@.RegisterDate smalldatetime,
@.LoginIDExists nvarchar(30)
AS
/* Check to see if the loginID is already in use before attempting to save it.
We MUST have a unique loginID for each user */
SELECT @.LoginIDExists = loginID FROM users WHERE loginID = @.LoginID
/* If we pulled a value from the database, then the loginID already exists, return with error code 1 */
IF (@.LoginIDExists = @.LoginID)
BEGIN
SELECT 1
RETURN
END
ELSE BEGIN
/* The loginID does not already exist, attemp to add the new user to the database. */
INSERT INTO users (
loginID,
loginpassword,
registerDate )
VALUES (
@.LoginID,
@.Password,
@.RegisterDate )
/* Grab the UserID for the new user. */
SELECT @.UserID = @.@.identity
/* return with error code 0 */
SELECT 0
RETURN
END
GO
[/pre]
I think it's ok, only some suggestions:
Replace the @.@.IDENTITY toSCOPE_IDENTITY()
Instead of using the SELECT statement to return the error code, you can use the RETURN statement. If you use the RETURN statement, you need to add a parameter and set the ParameterDirection to ReturnValue to the ADO.Net Command object.
|||Personally, I'd simplify it a bit more by removing the @.LoginExistsparameter. I'd take Fredrik's suggestions, plus I'd do the RETURNvalues slightly differently, and I'd do an @.@.ERROR check:CREATE PROCEDURE CreateNewUser
@.UserID int OUTPUT,
@.LoginID nvarchar(30),
@.Password nvarchar(30),
@.RegisterDate smalldatetime
AS
DECLARE @.Result INT
/* Check to see if the loginID is already in use before attempting to save it.
We MUST have a unique loginID for each user */
IF EXISTS(SELECT loginID FROM users WHERE loginID = @.LoginID)
/* If we pulled a value from the database, then the loginID already exists, return with error code -1 */
BEGIN
SELECT @.RESULT = -1
END
ELSE
BEGIN
/* The loginID does not already exist, attempt to add the new user to the database. */
INSERT INTO users (
loginID,
loginpassword,
registerDate )
VALUES (
@.LoginID,
@.Password,
@.RegisterDate )
/* Grab the @.@.Error and the UserID for the new user. */
SELECT @.Result = @.@.Error, @.UserID = Scope_Identity()
END
/* @.Result = -1 if the user was already on file, 0 if the user was added, and something else if the
user was attempted to be inserted and the INSERT failed */
RETURN @.Result
CREATE PROCEDURE CreateNewUser
@.UserID int OUTPUT,
@.LoginID nvarchar(30),
@.Password nvarchar(30),
@.RegisterDate smalldatetime
AS
DECLARE @.Result INT
/* Check to see if the loginID is already in use before attempting to save it.
We MUST have a unique loginID for each user */
BEGIN TRANSACTION
IF EXISTS(SELECT loginID FROM usersWITH (UPDLOCK) WHERE loginID = @.LoginID)
/* If we pulled a value from the database, then the loginID already exists, return with error code -1 */
BEGIN
SELECT @.RESULT = -1
END
ELSE
BEGIN
/* The loginID does not already exist, attempt to add the new user to the database. */
INSERT INTO users (
loginID,
loginpassword,
registerDate )
VALUES (
@.LoginID,
@.Password,
@.RegisterDate )
/* Grab the @.@.Error and the UserID for the new user. */
SELECT @.Result = @.@.Error, @.UserID = Scope_Identity()
END
/* @.Result = -1 if the user was already on file, 0 if the user was added, and something else if the
user was attempted to be inserted and the INSERT failed . COMMIT the transaction if there
were no errors, otherwise, ROLLBACK */
BEGIN
COMMIT
END
ELSE
BEGIN
ROLLBACK
END
RETURN @.Result|||
Thank's that's pretty cool. I need to get a book on SQL Server so I can learn to do all this stuff better.
I copied the code so I can modify my procedure later. I was going to make one procedure for each function, like CreateUser, EditUser, DeleteUser, ChangePassword, etc...
Do you think this is a good idea or should I include them all in one procedure.
I wanted to keep them small and simple. I didn't want to end up with a 100+ line procedure. I figured it would be better to do everything seperatly, and there's less code to run on each procedure so maybe they will be a little faster.
Thanks.
|||
Bluebarry wrote:
I copied the code so I can modify my procedure later. I was going tomake one procedure for each function, like CreateUser, EditUser,DeleteUser, ChangePassword, etc...
Personally, I like separate procedures. I think each storedprocedure should have one function. But if you have a lotof tables, that could mean a lot of lot of stored procedures.|||
tmorton wrote:
Bluebarry wrote:
I copied the code so I can modify my procedure later. I was going to make one procedure for each function, like CreateUser, EditUser, DeleteUser, ChangePassword, etc...
Personally, I like separate procedures. I think each stored procedure should have one function. But if you have a lot of tables, that could mean a lot of lot of stored procedures.
I dont care too much how many procedures i have unless its going to effect performance. i think having one large procedure would hurt performance more then several smaller procedures. That single procedure has to run more lines of code and do more calculations, like (If we are inserting to this, otherwise do this)... I write my programs in the same way I create many small simple methods rather then a few large methods. It makes it easier to keep bugs out of the program too.