Solved

Params not declared?  T-SQL syntax question

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL profiler equivalent in MS-Access 3 58
SQL Server - SQL field is defined as text 3 36
Help  needed 3 23
Trying to get a Linked Server to Oracle DB working 21 57
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…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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