Solved

Params not declared?  T-SQL syntax question

Posted on 2016-11-23
3
57 Views
Last Modified: 2016-11-28
I am running a small SQL statement to gather some missing data before it is passed to a stored procedure (  named "DSuEntity"  below)

The parameters passed into the stored proc "DSuEntity" - apparently there is a problem with me passing in values from the columns in my select statement right before the call to the SP.

What am I doing wrong?  Not the best at T-SQL, sorry.  I didn't write it, just trying to modify it.

param problem



Begin  
    /* Call procedure to Update a record into the Entity Table */  
    If @tiEntityType = 2  /*Individual */  
    Begin  

		/* Ensure that fields not provided on the Contact page 
		do not wipe out existing data.  For example, if Address3 is 
		not provided on the "Add Contact to Vehicle page" BUT it DOES
		have a value in the Entity table, then this update would wipe out
		the Address3 value!*/


		select e.vchAddress2, e.vchAddress3,  e.vchFaxNumber, e.vchSpecialNote1 from dbo.Entity e
		where e.iEntityId = @iEntityid;

        Exec @iReturnValue =  DSuEntity  
                    @iSiteId,                  
                    @iEntityid,               /*iEntityId will get set on Insert from Identity Column' */  
                    @tiEntityType,       /*Company = 1 and Individual = 2*/   
                    @vchCompanyName,         
                    @vchSalutation,     
                    @chGender,      
                    @vchFirstName,       
                    @vchMiddleName,     
                    @vchLastName,      
                    @vchAddress1,
                    e.vchAddress2,             /* from the select statement above */
                    e.vchAddress3,             /* from the select statement above */ 
                    @vchCity,                 

...


...

Open in new window

0
Comment
Question by:knowlton
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 499 total points
ID: 41899631
The return set from the SELECT statement will not be in scope (i.e. available) as parameters for the exec.  Also, if the SELECT returns more than one row, how does SQL know which values to use in the exec?

Give this a whirl, setting the result values of the SELECT as variables, then using those variables in the exec
Declare @vchAddress2 varchar(50), @vchAddress varchar(50) 

-- This SELECT statement ass-u-me's that only one row will be returned.
SELECT @vchAddress2  = e.vchAddress2, @vchAddress = e.vchAddress3
FROM dbo.Entity e
WHERE e.iEntityId = @iEntityid;

   Exec @iReturnValue =  DSuEntity  
                    @iSiteId,                  
                    @iEntityid,               /*iEntityId will get set on Insert from Identity Column' */  
                    @tiEntityType,       /*Company = 1 and Individual = 2*/   
                    @vchCompanyName,         
                    @vchSalutation,     
                    @chGender,      
                    @vchFirstName,       
                    @vchMiddleName,     
                    @vchLastName,      
                    @vchAddress1,
                    @vchAddress2,            
                   @vchAddress3,            
                    @vchCity, 

Open in new window


btw WHat's a DsuEntity ?
0
 
LVL 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 1 total points
ID: 41899967
Try...
Changed marked in bold.

Declare
        @vchAddress2 varchar(500)= ''
      , @vchAddress3 varchar(500) = ''

      
            SELECT TOP 1 @vchAddress2 = e.vchAddress2, @vchAddress3 = e.vchAddress3 from dbo.Entity e
            where e.iEntityId = @iEntityid


        Exec @iReturnValue =  DSuEntity  
                    @iSiteId,                  
                    @iEntityid,               /*iEntityId will get set on Insert from Identity Column' */  
...
....
                    @vchAddress2,             /* from the select statement above */
                    @vchAddress3,             /* from the select statement above */

                    @vchCity,                

...


...


Declare 
	  @vchAddress2 varchar(500)= ''
	, @vchAddress3 varchar(500) = ''
	
Begin  
    /* Call procedure to Update a record into the Entity Table */  
    If @tiEntityType = 2  /*Individual */  
    Begin  

		/* Ensure that fields not provided on the Contact page 
		do not wipe out existing data.  For example, if Address3 is 
		not provided on the "Add Contact to Vehicle page" BUT it DOES
		have a value in the Entity table, then this update would wipe out
		the Address3 value!*/


		SELECT TOP 1 @vchAddress2 = e.vchAddress2, @vchAddress3 = e.vchAddress3 from dbo.Entity e
		where e.iEntityId = @iEntityid

        Exec @iReturnValue =  DSuEntity  
                    @iSiteId,                  
                    @iEntityid,               /*iEntityId will get set on Insert from Identity Column' */  
                    @tiEntityType,       /*Company = 1 and Individual = 2*/   
                    @vchCompanyName,         
                    @vchSalutation,     
                    @chGender,      
                    @vchFirstName,       
                    @vchMiddleName,     
                    @vchLastName,      
                    @vchAddress1,
                    @vchAddress2,             /* from the select statement above */
                    @vchAddress3,             /* from the select statement above */ 
                    @vchCity,                 

...


...

Open in new window

0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 41904529
Thanks!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question