Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Params not declared?  T-SQL syntax question

Posted on 2016-11-23
3
Medium Priority
?
97 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 1996 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 30

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 4 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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…

722 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