Solved

Can I select a field value into a @variable so I can use it in my next Insert statement?

Posted on 2008-10-16
2
260 Views
Last Modified: 2012-05-05
Hi, I'm currently getting error in the following code for 'interestName'.  It doesn't like me using it in the Insert statement.  Do I need to select into a @variable?  What's the syntax like?  Thank you.

Msg 128, Level 15, State 1, Procedure GetData, Line 85
The name "interestName" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
SELECT dbo.ResearchInterests.Name as interestName, dbo.ResearchInterests.ParentResearchInterest_id as parentId
	from dbo.ResearchInterests
	WHERE dbo.ResearchInterests.Id = @interestChildId
 
INSERT INTO [CtsiMembershipProduction].[dbo].[Interest]
           ([Name])
     VALUES
           (interestName)

Open in new window

0
Comment
Question by:lapucca
[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
2 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 22737189
INSERT INTO [CtsiMembershipProduction].[dbo].[Interest] ([Name])
SELECT dbo.ResearchInterests.Name
      from dbo.ResearchInterests
      WHERE dbo.ResearchInterests.Id = @interestChildId
0
 

Author Comment

by:lapucca
ID: 22737229
I see but I also need the paretnId.  I need it for my innner while condition, while parentId <> NULL.  Can I select the value into a @Local_variable?  If not, What can I do here to get both selected values?  Thank you.

WHILE @@FETCH_STATUS =0  --GO THROUGH EACH RECORD
BEGIN
      --The following select will get us the 1st child id for Rsch interest
      SELECT dbo.ResearchInterests.Name AS interestName, dbo.ResearchInterests.ParentResearchInterest_id as parentId
      from dbo.ResearchInterests
      WHERE dbo.ResearchInterests.Id = @interestChildId

INSERT INTO [CtsiMembershipProduction].[dbo].[Interest]([Name])
     VALUES
           (interestName)

      WHILE(parentId <> NULL)--USE THE PARENTID TO TRAVERSE TO ITS PARENT NODE UNTIL REACHES ROOT NODE
      BEGIN
            set @interestChildId = parentId
            SELECT dbo.ResearchInterests.Name, dbo.ResearchInterests.ParentResearchInterest_id as parentId
            from dbo.ResearchInterests
            WHERE dbo.ResearchInterests.Id = @interestChildId
      END

      FETCH NEXT FROM curApplicationRow
      INTO @interestChildId
END
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
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.
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…

688 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