|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, November 05, 2012 1:26 PM
Points: 7,
Visits: 8
|
|
Good day all!!!
I have just started learning about CLR Stored Procedures and I wanted to know how to convert my existing stored procedures to CLR Stored Procedures. Could you show me, with code, what this stored procedure should look like. I'm using VS 2010, VB.Net, MSSQL Server 2008.
ALTER PROCEDURE [dbo].[user_login] -- Add the parameters for the stored procedure here @User_UserName VARCHAR(50), @User_Password VARCHAR(50), @Station VARCHAR(50), @Users_RowID INT OUTPUT, @Users_Name VARCHAR(100) OUTPUT, @Success BIT OUTPUT, @Default_Message VARCHAR(200) OUTPUT AS
BEGIN TRANSACTION -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT OFF;
DECLARE @rowsaffected INT DECLARE @User_Status_RowID INT DECLARE @User_Cursor CURSOR
-- Insert statements for procedure here SET @Success = 0 SET @Users_RowID = 0 SET @Users_Name = '' SET @User_Status_RowID = 0 IF EXISTS (SELECT * FROM [dbo].[user] WHERE User_UserName = @User_UserName AND CONVERT(varbinary, User_Password) = CONVERT(varbinary, @User_Password)) BEGIN SELECT @Users_RowID = User_RowID, @User_Status_RowID = User_Status_RowID FROM dbo.[user] WHERE User_UserName = @User_UserName AND User_Password = @User_Password SELECT @Users_Name = (User_SName + ', ' + User_FName + '. ' + Title_Descr) FROM dbo.[user] INNER JOIN dbo.title ON (dbo.[user].Title_RowID = dbo.title.Title_RowID) WHERE User_UserName = @User_UserName AND User_Password = @User_Password
IF ((LOWER(@User_UserName) <> 'administrator') AND (@User_Status_RowID = 1)) BEGIN SET @Default_Message = 'User ''' + @Users_Name + ''' is already logged in.' SET @Success = 0 RETURN END ELSE BEGIN EXEC [dbo].[user_status_update] @User_UserName, @Station, 1, @rowsaffected OUTPUT IF ((@rowsaffected = 2) AND (@@ERROR = 0)) BEGIN SET @Default_Message = 'You have been logged in successfully' SET @Success = 1 END ELSE BEGIN ROLLBACK TRANSACTION SET @Default_Message = 'An error occured while attempting to log you in. Please try again' SET @Success = 0 RETURN END END END ELSE BEGIN ROLLBACK TRANSACTION SET @Default_Message = 'Invalid username and/or password. Try again' SET @Success = 0 RETURN END
SET NOCOUNT ON;
COMMIT TRANSACTION
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Today @ 3:02 AM
Points: 484,
Visits: 2,127
|
|
Why do you want to convert this to CLR? Unless I'm missing something, I don't see what a CLR version of this sproc is going to give you that a T-SQL version doesn't do.
----------------------------------- http://www.SQL4n00bs.com
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, November 05, 2012 1:26 PM
Points: 7,
Visits: 8
|
|
I am developing an application that is going to be setup on a server that is in a public area, that anyone will have access to. My thinking was that CLR Stored Procedures are more secure than the regular stored procedures because they can't be edited from the server. The stored procedure I posted is the log in procedure I'm using. However the other procedures I have are more complex, carrying out billing and remittance logic, etc. So I wanted to hide the logic from the anyone who might try to make unauthorized changes. I posted the simplest stored procedure so that I could learn how to implement it starting with the basics.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Yesterday @ 3:38 AM
Points: 803,
Visits: 2,123
|
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 3:52 AM
Points: 11,613,
Visits: 27,670
|
|
azinyama (11/5/2012)
I am developing an application that is going to be setup on a server that is in a public area, that anyone will have access to.
If you are trying to block the DBA from seeing the CLR body, you are just making it more difficult...if the CLR is deployed, i can still extract the assembly and run it through a decompiler.
Now for end users, SQL server is deny by default...that means no one can do anything unless you give them permission to do something, so it's really advantageous to learn how to be tight with permissions instead of granting db_owner to everyone, for example.
So for example, you can give people EXECUTE permissions on a stored procedure(s), and all they can do is call it,they cannot see the procedure body unless you give them permission to do so.(db_ddladmin,db_owner, VIEW DEFINITION)
My thinking was that CLR Stored Procedures are more secure than the regular stored procedures because they can't be edited from the server.
you are introducing a big performance hit by trying to do everything in CLR...the code you posted already has a cursor in it, which is something you want to avoid up front, and use set based operations to take advantage of SQL's abilities.
The stored procedure I posted is the log in procedure I'm using. However the other procedures I have are more complex, carrying out billing and remittance logic, etc. So I wanted to hide the logic from the anyone who might try to make unauthorized changes. I posted the simplest stored procedure so that I could learn how to implement it starting with the basics.
CLR's have their place, but they should be reserved to do things you cannot normally do in TSQL..if you relpace existing functionality with a CLR, it will be slower, with very very rare exceptions.
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 3:33 AM
Points: 2,533,
Visits: 4,346
|
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Yesterday @ 3:38 AM
Points: 803,
Visits: 2,123
|
|
The problem is that the encryption is easily reversed if you're determined enough, it might stop a casual person but anyone with any tenacity or google will be able to bybass that quickly once they have a script.
This is one thread on MSDN about it though it appears that you need to have Admin rights.
http://social.msdn.microsoft.com/Forums/en/transactsql/thread/e7056ca8-94cd-4d36-a676-04c64bf96330
Lowel is correct you're better using Permissions to prevent this from happening and if it does happen using an Database trigger and Audit to trace changes so you know who to point the finger at and prove that someone has done something they shouldnt.
_________________________________________________________________________ SSC Guide to Posting and Best Practices
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, November 05, 2012 1:26 PM
Points: 7,
Visits: 8
|
|
Thank you all for the advice. Will look into permissions option...
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, November 05, 2012 1:26 PM
Points: 7,
Visits: 8
|
|
Another question though...
What would I then do about preventing people from using Windows Authentication to log in???
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Today @ 3:02 AM
Points: 484,
Visits: 2,127
|
|
When you create the SQL Server logins, specify SQL Server Authentication only?
However, windows authentication (when configured correctly) is very secure and will be less headache for your DBAs
----------------------------------- http://www.SQL4n00bs.com
|
|
|
|