Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL - Write Query with field names and table name stored in a table?

SQL - Write Query with field names and table name stored in a table?

Scheduled Pinned Locked Moved Database
databaseperformancetutorialquestionannouncement
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Megan Jean
    wrote on last edited by
    #1

    I work for an Insurance Company and creating a custom application. When we apply a coverage to a policy it is stored in a table with a Limit, Deductible, and Premium field. However, there are many coverages that do not follow the norm. They use different fields for Limit, Deductible, and Premium and are even stored in different tables. (Don’t ask why and don’t even try to understand the fact that they are STILL doing this so I need to leave logic to allow for additional coverages like this.) There is also no rhyme or reason to what fields they choose so varies greatly. So, we have decided to create “LookUp” tables that will store all the information we need for a coverage. This avoids numerous joins – we were having issues with speed and performance of the application. I need to find a way to prepopulate all these fields into a table. I would like the best option performance-wise. This will end up being a Nightly job – so when we receive a new snapshot of data we will run this query and prepopulate/update the values of the table. Here is an example of my table, it contains the Coverage name (Coverage), the table (File), and the fields for the Limit, Deductible, and Premium. The PolicyNum field is a concatenation of LOB + Policy + Module. I need all three of these fields to join to a policy. I wanted to use a table since they will be adding new coverages. Keep in mind, I created this tables so can add more fields to it. Here is the table I am referring to:

    USE [PolicySummary]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    CREATE TABLE \[dbo\].\[Coverage\](
    	\[CoverageID\] \[int\] IDENTITY(1,1) NOT NULL,
    	\[LOB\] \[nvarchar\](255) NULL,
    	\[Coverage\] \[nvarchar\](255) NULL,
    	\[Description\] \[nvarchar\](255) NULL,
    	\[File\] \[nvarchar\](255) NULL,
    	\[Limit\] \[nvarchar\](255) NULL,
    	\[Deductible\] \[nvarchar\](255) NULL,
    	\[Premium\] \[nvarchar\](255) NULL,
    	\[IsMultLimit\] \[bit\] NULL,
    	\[IsMultDeductible\] \[bit\] NULL,
    	\[IsMultPremium\] \[bit\] NULL,
    	\[InsuranceLine\] \[nvarchar\](255) NULL,
    	\[PolicyNum\] \[nvarchar\](255) NULL,
     CONSTRAINT \[PK\_Coverage\] PRIMARY KEY CLUSTERED 
    (
    	\[CoverageID\] ASC
    )WITH (PAD\_INDEX  = OFF, STATISTICS\_NORECOMPUTE  = OFF,     IGNORE\_DUP\_KEY = OFF, ALLOW\_ROW\_LOCKS  = ON, ALLOW\_PAGE\_LOCKS  =     ON) ON \[PRIMARY\]
    ) ON \[PRIMARY\]
    
    GO
    

    So in a nutshell, a policy may have the coverage BP0713. I then to look at this table and see that I need t

    P G 2 Replies Last reply
    0
    • M Megan Jean

      I work for an Insurance Company and creating a custom application. When we apply a coverage to a policy it is stored in a table with a Limit, Deductible, and Premium field. However, there are many coverages that do not follow the norm. They use different fields for Limit, Deductible, and Premium and are even stored in different tables. (Don’t ask why and don’t even try to understand the fact that they are STILL doing this so I need to leave logic to allow for additional coverages like this.) There is also no rhyme or reason to what fields they choose so varies greatly. So, we have decided to create “LookUp” tables that will store all the information we need for a coverage. This avoids numerous joins – we were having issues with speed and performance of the application. I need to find a way to prepopulate all these fields into a table. I would like the best option performance-wise. This will end up being a Nightly job – so when we receive a new snapshot of data we will run this query and prepopulate/update the values of the table. Here is an example of my table, it contains the Coverage name (Coverage), the table (File), and the fields for the Limit, Deductible, and Premium. The PolicyNum field is a concatenation of LOB + Policy + Module. I need all three of these fields to join to a policy. I wanted to use a table since they will be adding new coverages. Keep in mind, I created this tables so can add more fields to it. Here is the table I am referring to:

      USE [PolicySummary]
      GO
      SET ANSI_NULLS ON
      GO
      SET QUOTED_IDENTIFIER ON
      GO

      CREATE TABLE \[dbo\].\[Coverage\](
      	\[CoverageID\] \[int\] IDENTITY(1,1) NOT NULL,
      	\[LOB\] \[nvarchar\](255) NULL,
      	\[Coverage\] \[nvarchar\](255) NULL,
      	\[Description\] \[nvarchar\](255) NULL,
      	\[File\] \[nvarchar\](255) NULL,
      	\[Limit\] \[nvarchar\](255) NULL,
      	\[Deductible\] \[nvarchar\](255) NULL,
      	\[Premium\] \[nvarchar\](255) NULL,
      	\[IsMultLimit\] \[bit\] NULL,
      	\[IsMultDeductible\] \[bit\] NULL,
      	\[IsMultPremium\] \[bit\] NULL,
      	\[InsuranceLine\] \[nvarchar\](255) NULL,
      	\[PolicyNum\] \[nvarchar\](255) NULL,
       CONSTRAINT \[PK\_Coverage\] PRIMARY KEY CLUSTERED 
      (
      	\[CoverageID\] ASC
      )WITH (PAD\_INDEX  = OFF, STATISTICS\_NORECOMPUTE  = OFF,     IGNORE\_DUP\_KEY = OFF, ALLOW\_ROW\_LOCKS  = ON, ALLOW\_PAGE\_LOCKS  =     ON) ON \[PRIMARY\]
      ) ON \[PRIMARY\]
      
      GO
      

      So in a nutshell, a policy may have the coverage BP0713. I then to look at this table and see that I need t

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Where is the table? :confused: It sounds reasonable so far.

      You'll never get very far if all you do is follow instructions.

      M M 2 Replies Last reply
      0
      • P PIEBALDconsult

        Where is the table? :confused: It sounds reasonable so far.

        You'll never get very far if all you do is follow instructions.

        M Offline
        M Offline
        Megan Jean
        wrote on last edited by
        #3

        I have edited my question to include the script I use to create the table. Sorry about that.

        P 1 Reply Last reply
        0
        • M Megan Jean

          I work for an Insurance Company and creating a custom application. When we apply a coverage to a policy it is stored in a table with a Limit, Deductible, and Premium field. However, there are many coverages that do not follow the norm. They use different fields for Limit, Deductible, and Premium and are even stored in different tables. (Don’t ask why and don’t even try to understand the fact that they are STILL doing this so I need to leave logic to allow for additional coverages like this.) There is also no rhyme or reason to what fields they choose so varies greatly. So, we have decided to create “LookUp” tables that will store all the information we need for a coverage. This avoids numerous joins – we were having issues with speed and performance of the application. I need to find a way to prepopulate all these fields into a table. I would like the best option performance-wise. This will end up being a Nightly job – so when we receive a new snapshot of data we will run this query and prepopulate/update the values of the table. Here is an example of my table, it contains the Coverage name (Coverage), the table (File), and the fields for the Limit, Deductible, and Premium. The PolicyNum field is a concatenation of LOB + Policy + Module. I need all three of these fields to join to a policy. I wanted to use a table since they will be adding new coverages. Keep in mind, I created this tables so can add more fields to it. Here is the table I am referring to:

          USE [PolicySummary]
          GO
          SET ANSI_NULLS ON
          GO
          SET QUOTED_IDENTIFIER ON
          GO

          CREATE TABLE \[dbo\].\[Coverage\](
          	\[CoverageID\] \[int\] IDENTITY(1,1) NOT NULL,
          	\[LOB\] \[nvarchar\](255) NULL,
          	\[Coverage\] \[nvarchar\](255) NULL,
          	\[Description\] \[nvarchar\](255) NULL,
          	\[File\] \[nvarchar\](255) NULL,
          	\[Limit\] \[nvarchar\](255) NULL,
          	\[Deductible\] \[nvarchar\](255) NULL,
          	\[Premium\] \[nvarchar\](255) NULL,
          	\[IsMultLimit\] \[bit\] NULL,
          	\[IsMultDeductible\] \[bit\] NULL,
          	\[IsMultPremium\] \[bit\] NULL,
          	\[InsuranceLine\] \[nvarchar\](255) NULL,
          	\[PolicyNum\] \[nvarchar\](255) NULL,
           CONSTRAINT \[PK\_Coverage\] PRIMARY KEY CLUSTERED 
          (
          	\[CoverageID\] ASC
          )WITH (PAD\_INDEX  = OFF, STATISTICS\_NORECOMPUTE  = OFF,     IGNORE\_DUP\_KEY = OFF, ALLOW\_ROW\_LOCKS  = ON, ALLOW\_PAGE\_LOCKS  =     ON) ON \[PRIMARY\]
          ) ON \[PRIMARY\]
          
          GO
          

          So in a nutshell, a policy may have the coverage BP0713. I then to look at this table and see that I need t

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #4

          Don't forget to create some indexes too, in order to speed things up.

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          1 Reply Last reply
          0
          • M Megan Jean

            I have edited my question to include the script I use to create the table. Sorry about that.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Ah, good, how about some sample data? Made up values would be fine of course.

            You'll never get very far if all you do is follow instructions.

            M 1 Reply Last reply
            0
            • P PIEBALDconsult

              Ah, good, how about some sample data? Made up values would be fine of course.

              You'll never get very far if all you do is follow instructions.

              M Offline
              M Offline
              Megan Jean
              wrote on last edited by
              #6

              I had issues pasting my entire query so I just pasted the piece inside the loop. It works but seriously takes ways too long. I need to find a way from looping through them all and probably not using dynamic SQL but can't figure out a way yet.

              P 1 Reply Last reply
              0
              • M Megan Jean

                I had issues pasting my entire query so I just pasted the piece inside the loop. It works but seriously takes ways too long. I need to find a way from looping through them all and probably not using dynamic SQL but can't figure out a way yet.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                I can probably help a bit once I can get other things off my mind. Yes, removing the looping is likely to help quite a bit.

                You'll never get very far if all you do is follow instructions.

                1 Reply Last reply
                0
                • P PIEBALDconsult

                  Where is the table? :confused: It sounds reasonable so far.

                  You'll never get very far if all you do is follow instructions.

                  M Offline
                  M Offline
                  Member 10714909
                  wrote on last edited by
                  #8

                  Dear if you want to learn sql by online free tutorial. this tutorial may be helpful to you. www.javatpoint.com/sql-tutorial post your queries on javatpoint forum. www.javatpoint.com/forum

                  P 1 Reply Last reply
                  0
                  • M Member 10714909

                    Dear if you want to learn sql by online free tutorial. this tutorial may be helpful to you. www.javatpoint.com/sql-tutorial post your queries on javatpoint forum. www.javatpoint.com/forum

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Probably wouldn't help me, but maybe the OP.

                    You'll never get very far if all you do is follow instructions.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups