Solved

How do i fix this code to work

Posted on 2011-03-01
7
201 Views
Last Modified: 2012-05-11
I have this Stored Procedure I am writing, and i need to get the Database name from a table to run another query from that stored database name. how can i fix this.

declare cur1 cursor for
SELECT [id],[Source_Interid],[From_Vendor_ID],[To_Vendor_ID] FROM [CE_CUSTOM].[dbo].[dw_Vendor_Merge] Where processed_flag='N'
            
open cur1
fetch next from cur1 into @id, @Company, @Old_Vendor_ID, @New_Vendor_ID
set @Company=ltrim(rtrim(@Company))
while @@FETCH_STATUS = 0
begin


THIS IS THE PROBLEM LINE OF CODE.

              -- Get the Vedors New Name
    Select @New_Vendor_Name = select VENDNAME from @Company..pm00200 where VENDORID = @New_Vendor_ID

End line of code NOTE WHERE IT SAYS @COMPANY NAME THAT IS THE PROBLEM.



 etch next from cur1 into @id, @Company, @Old_Vendor_ID, @New_Vendor_ID
 end
close cur1
deallocate cur1
0
Comment
Question by:Steve Samson
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
Comment Utility
Replace
Select @New_Vendor_Name = select VENDNAME from @Company..pm00200 where VENDORID = @New_Vendor_ID
to
Select @New_Vendor_Name = VENDNAME from @Company..pm00200 where VENDORID = @New_Vendor_ID
0
 

Author Comment

by:Steve Samson
Comment Utility
My bad on this, but I had paste that in by mistake. I ment to say exactly what you stated but the problem is this part @Company..pm00200  of  the statement?

 Select @New_Vendor_Name = VENDNAME from @Company..pm00200 where VENDORID = @New_Vendor_ID

I get this error
Msg 102, Level 15, State 1, Procedure ce_sp_DW_Vendor_Merge, Line 40
Incorrect syntax near '.'.
0
 
LVL 9

Accepted Solution

by:
rajeevnandanmishra earned 500 total points
Comment Utility
Hi,

You need to use the dynamic execution of code. Something like below may be useful.
Please add below code with your code and give it a try.

 
-- Declare a SQL Statement Variable
declare @strSql nvarchar(1000)

-- In the loop of your code assign the value into the @strSql 
select @strSql = 'SELECT @New_Vendor_Name = VENDNAME FROM ' + @Company + '..pm00200 WHERE VENDORID = @New_Vendor_ID'
-- Now execute the below statement. You may need to change the datatype or length
exec sp_executesql @strSql, N'@New_Vendor_Name VARCHAR(50) OUTPUT, @New_Vendor_ID VARCHAR(6)',  @New_Vendor_Name OUTPUT, @New_Vendor_ID 

-- At this point you are having the proper value in the variable @New_Vendor_Name

Open in new window


0
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.

 
LVL 10

Expert Comment

by:Jacco
Comment Utility
Hi,

I think you want something along these lines:

declare @ParameterDefinition nvarchar(500) = '@New_Vendor_Name_OUT nvarchar(100) OUTPUT'
declare @New_Vendor_Name nvarchar(100)

declare @qry nvarchar(4000) = 'Select @New_Vendor_Name_OUT = (select VENDNAME from ' + @Company + '..pm00200 where VENDORID = ' + convert(varchar, @New_Vendor_ID) + ')'

exec sp_executesql  @qry, @ParameterDefinition, @New_Vendor_Name_OUT = @New_Vendor_Name OUTPUT

Regards, Jacco
0
 
LVL 10

Expert Comment

by:Jacco
Comment Utility
lol, 6 minutes :-)
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
Comment Utility
I agree with rajeevnandanmishra's comment.
@Company is a variable. It cannot be appended directly to actual query string

SELECT * FROM @Company..pm00200
will be executed as it is. Since there is not database with name '@Company' it will be an error
Instead we want the value of that variable
So using dynamic query, it would work
@Company + '..pm00200' will become something like somedbname..pm00200'

Hope it is clear
Raj
0
 

Author Closing Comment

by:Steve Samson
Comment Utility
Work great
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

763 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

12 Experts available now in Live!

Get 1:1 Help Now