Solved

Params not declared?  T-SQL syntax question

Posted on 2016-11-23
3
84 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:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 66

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 29

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:Tom Knowlton
ID: 41904529
Thanks!
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 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