views:

64

answers:

4

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.

A: 

That says to me something is funny in your application code. I would check your C# logic and make sure you are using the right function call on the command object. It might help to post your code.

Cypher
There is no application code. Just the trigger, stored procedure, and user defined function.
So what's executing the sproc in production then?
annakata
A trigger on a table.
+1  A: 

While it's impossible to see what is happening without table structures, sprocs and functions my thoughts would be to compare the table structures, defaults, identity columns, etc. in PROD and DEV.

Nissan Fan
There is only one database.
The other issue you may be facing is the fact that default values are handled differently. You may have a nullable column in SQL Server which defaults to 0, but with .NET you have to set it to 0 to use that default otherwise it will be NULL.
Nissan Fan
There is no .NET involved.
A: 

As Nissan Fan says, you haven't provided nearly enough information. That said, one possibility is that you're on SQL Server 2000 and are encountering an old bug. If you want a useful answer, though, ask a useful question.

Steve Kass
Using SQL Server 2005. I wasn't really expecting a useful answer, just checking to see if there was anything obvious that I was missing since I'm out of my element on this one.
+1  A: 

Get SQL Profiler on it and then copy & paste & execute the statements from that in query analyser.

It's likely that your production is producing subtlely different code or is injecting different param values than the ones you expect and this will catch exactly what is happening.

annakata
Does Profiler trace stored procedure calls from a trigger? I have tried running Profiler, but it doesn't seem to be capturing the sproc or function calls I need to look at. They are actually executing though.
I believe it doesn't by default but can do. Offhand can't remember how, the thing isn't the most user-friendly.
annakata
I did figure this part out. When you start a new trace, in the Events Selection tab, check "show all events" and then under Stored Procedures, check SP:StmtCompleted and SP:StmtStarting. Thanks for your help annakata. I'm tracing the stored procedure and function, but still working out how to tell what values are being passed.
Annakata, you got me closest to the solution so thanks for that. I ended up creating a new stored procedure (with the exact same code), executing the new one and deleting the old one. It bugs me because I still don't know *why*, but at least it is resolved.