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. General Programming
  3. C#
  4. Parse SQL and replace column name with another name

Parse SQL and replace column name with another name

Scheduled Pinned Locked Moved C#
questiondatabasehelp
3 Posts 2 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.
  • A Offline
    A Offline
    AhmedMasum
    wrote on last edited by
    #1

    Suppose the sql is :

    select empno, empname, deptname,sal from emp, dept where emp.did= dept.did

    I need the following: If the column name is empno replace it by eno, If the table name is emp then replace it by employee, If the table is dept then replace it by department, if the column name is did replace it by deptid. That means after operation my the sql should be as follows:

    select eno, empname, deptname, sal from employee,department where employee.deptid=department.deptid

    I will be greatfull if any body help me to complete this. I got a parser but it only can parse the column name in select statement and table names. But I am not getting the column names in the join condition. After getting the column name or table name how can I replce those? Suppose I got emp as table but if I replace all the emp by employee then empname is also converting to employeename which is becoming to wrong column name

    P 1 Reply Last reply
    0
    • A AhmedMasum

      Suppose the sql is :

      select empno, empname, deptname,sal from emp, dept where emp.did= dept.did

      I need the following: If the column name is empno replace it by eno, If the table name is emp then replace it by employee, If the table is dept then replace it by department, if the column name is did replace it by deptid. That means after operation my the sql should be as follows:

      select eno, empname, deptname, sal from employee,department where employee.deptid=department.deptid

      I will be greatfull if any body help me to complete this. I got a parser but it only can parse the column name in select statement and table names. But I am not getting the column names in the join condition. After getting the column name or table name how can I replce those? Suppose I got emp as table but if I replace all the emp by employee then empname is also converting to employeename which is becoming to wrong column name

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

      Use a Dictionary<string,string> ? Populate it with the old and new values. Split the existing statement and iterate the parts. If a part exists in the dictionary retrieve its replacement. Build a new statement.

      A 1 Reply Last reply
      0
      • P PIEBALDconsult

        Use a Dictionary<string,string> ? Populate it with the old and new values. Split the existing statement and iterate the parts. If a part exists in the dictionary retrieve its replacement. Build a new statement.

        A Offline
        A Offline
        AhmedMasum
        wrote on last edited by
        #3

        I got some idea. But do you know any god parsers name where i can easily find out the column names, table names, join tables names columns in join condition, column in where clause, column in group by clause and having clause.

        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