I have a stored procedure that works correctly when I execute the stored procedure from SQL Server Management Studio. Unfortunately, it doesn't behave the the same way on the production server. On an insert statement, it is only inserting some of the values and the rest are NULL. The NULL values are coming from user defined scalar function calls (which also work correctly when executed from Management Studio). Has anyone run into anything similar? I was thinking it might be a permissions issue, but I connected to the database through Management Studio with the production connection credentials and saw the same behavior. I'm a C# developer that normally works with ORMs, so I'm definitely no SQL expert. Thanks in advance, guys.
Here is the code:
BEGIN
DECLARE @UserExists int
SET @UserExists = 0
SELECT @UserExists = COUNT(*) FROM UserPass WHERE UserId = @UserID AND PortalID = @PORTALID
--If the Action is add and a User Exists change the Action to EDT (Update)
IF @Action = 'ADD' AND @UserExists > 0
SET @Action = 'EDT'
--Get All Of the Properties for this User
DECLARE @EMAIL nvarchar(255)
DECLARE @FIRSTNAME nvarchar(50)
DECLARE @LASTNAME nvarchar(50)
DECLARE @GENDER char(1)
DECLARE @BIRTHDATE smalldatetime
DECLARE @ADDRESS nvarchar(50)
DECLARE @CITY nvarchar(50)
DECLARE @STATE nchar(2)
DECLARE @COUNTRY nvarchar(50)
DECLARE @POSTALCODE nvarchar(10)
DECLARE @TELEPHONE nvarchar(20)
DECLARE @CELL nvarchar(20)
DECLARE @EMAILPERMISSION bit
DECLARE @TEXTPERMISSION bit
DECLARE @UPDATEDIRECTION nvarchar(3)
BEGIN TRY
SELECT @BIRTHDATE = CAST(dbo.GetPropertyValue(@PORTALID,@USERID,'Birthdate') AS SmallDatetime)
END TRY
BEGIN CATCH
SELECT @BIRTHDATE = NULL
END CATCH
SELECT @EMAIL = Email,
@FIRSTNAME = dbo.Proper(Firstname),
@LASTNAME = dbo.Proper(Lastname),
@GENDER = dbo.GetPropertyValue(@PORTALID,@USERID,'Gender'),
@ADDRESS = dbo.GetPropertyValue(@PORTALID,@USERID,'Street'),
@CITY = dbo.Proper(dbo.GetPropertyValue(@PORTALID,@USERID,'City')),
@STATE = Upper(dbo.GetState(dbo.GetPropertyValue(@PORTALID,@USERID,'Region'))),
@COUNTRY = dbo.GetPropertyValue(@PORTALID,@USERID,'Country'),
@POSTALCODE = dbo.GetPropertyValue(@PORTALID,@USERID,'Postalcode'),
@TELEPHONE = dbo.STRFILTER(dbo.GetPropertyValue(@PORTALID,@USERID,'Telephone'),'0,1,2,3,4,5,6,7,8,9'),
@CELL = dbo.STRFILTER(dbo.GetPropertyValue(@PORTALID,@USERID,'Cell'),'0,1,2,3,4,5,6,7,8,9'),
@EMAILPERMISSION = dbo.GetPropertyValue(@PORTALID,@USERID,'eNewsLetter'),
@TEXTPERMISSION = dbo.GetPropertyValue(@PORTALID,@USERID,'TextPermission')
FROM Users
WHERE UserId = @USERID
-- Insert new user
IF @Action = 'ADD'
BEGIN
INSERT INTO UserPass
(UserID, Portalid, CreatedDate, Username, UserPass.Password, email, firstname, lastname, gender, birthdate, UserPass.address, city, UserPass.state, country, postalcode, telephone, cell, emailpermission, textpermission, UpdateDirection)
VALUES
(@UserID, @PORTALID, @CREATEDDATE, @Username, @Password, @EMAIL, @FIRSTNAME, @LASTNAME,@GENDER, @BIRTHDATE, @ADDRESS, @CITY, @STATE, @COUNTRY, @POSTALCODE, @TELEPHONE, @CELL, @EMAILPERMISSION, @TEXTPERMISSION, 'OUT')
END
@PORTALID and @USERID are passed to the stored procedure as parameters, and those values are actually saving in the insert. The columns that aren't updating are the ones that call the GetPropertyValue function for the value. This is only on one database server (I am not connecting to a dev database through Management studio, I am connecting directly to the production database). When I execute the stored procedure from Management Studio, it's perfect. When the trigger on the table calls the sproc, the GetPropertyValue function fails.