Solved

Params not declared?  T-SQL syntax question

Posted on 2016-11-23
3
18 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 17

Assisted Solution

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

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, …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now