Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 953013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:58:59+00:00 2026-05-15T23:58:59+00:00

So here’s the setup. I have two tables: CREATE TABLE dbo.TmpFeesToRules1(Name varchar, LookupId int)

  • 0

So here’s the setup. I have two tables:

CREATE TABLE dbo.TmpFeesToRules1(Name varchar, LookupId int)
CREATE TABLE dbo.TempFeesToRules2(FeeId int, Name varchar)

I have a third table called ‘Fee’ in the database that’s already created. I want to populate dbo.TmpFeesToRules1 ‘Name’ field with the DISTINCT ‘Name’ from ‘Fee’. Would I do this like this?

INSERT INTO dbo.TmpFeesToRules1(Name, LookupId)
VALUES (SELECT DISTINCT Name FROM Fee, 0)

Then I want to use a cursor to loop through dbo.TmpFeesToRules1 and insert each of these rows into another table called ‘Lookup’, so those names would then have LookupId’s assigned to them:

DECLARE db_cursor CURSOR FOR  
SELECT Name 
FROM dbo.TmpFeesToRules1 

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @Name   

WHILE @@FETCH_STATUS = 0   
BEGIN   
       INSERT INTO dbo.Lookup (LookupType, LookupDesc)
       VALUES ('FEE', @Name)

       FETCH NEXT FROM db_cursor INTO @name   
END   

CLOSE db_cursor

Then I want to come back to dbo.TmpFeesToRules1 and UPDATE it and insert those LookupId’s for each one of the names. How do I do this?

Also, I don’t think my SQL is entirely correct for everything else either? Can you guys verify this?

  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:58:59+00:00Added an answer on May 15, 2026 at 11:58 pm

    Instead of mucking about with cursors, I would just do the following:

    INSERT INTO dbo.TmpFeesToRules1 (name, LookupId)
    SELECT DISTINCT name, 0 FROM Fee
    
    INSERT INTO Lookup (LookupType, LookupDesc)
    SELECT 'FEE', name FROM dbo.TmpFeesToRules1
    
    UPDATE TFTR
    SET
        LookupID = LU.id
    FROM
        Lookup LU
    INNER JOIN dbo.TmpFeesToRules1 TFTR ON
        TFTR.name = LU.LookupDesc
    WHERE
        LU.LookupType = 'FEE'
    

    There are a lot of assumptions made here – for example that other FEE lookups with the same description don’t already exist.

    There seemed to be a lot of problems with your SQL syntax as well as your strategy here. If this is for a production system I strongly suggest that you find a SQL developer who know what he’s doing to handle these kinds of things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here is my code, which takes two version identifiers in the form 1, 5,
Here we go again, the old argument still arises... Would we better have a
Here's what I'm trying do to in a single SQL Server procedure: @ID1 int
Here is a link my example of the misaligned table rows Click preview in
Here’s a problem I’ve really been struggling with. I need to merge two sorted
Here, i have coded to get data from DB. I want to store the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.