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 6115829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:08:13+00:00 2026-05-23T15:08:13+00:00

I want a query that will return a row for each column in a

  • 0

I want a query that will return a row for each column in a view, and a row for the view itself.

There should be a column basetable in the result that gives the base table for the column in the current row, and a column basefield in the result that gives the name of the column in the underlying query (for renamed columns). It would be a bonus if any calculations could also be included in the basefield column.

I don’t think this can be done. Am I wrong?

In the example below “what goes here” should be replaced by table1 or table2 as appropriate in the basetable column, and a, b, or c as appropriate in the basefield column.

create table table1 (a int, b int)
create table table2 (a int, c int)
go
create view view1 as select table1.a, table1.b, table2.c from table1 left join table2 on table1.a = table2.a
go

select * from 
(
 select 'View' objecttype,O.name viewname,'' fieldname,0 column_id,'' typename,'' max_length,'' [precision], '' scale, '' is_identity,
    'what goes here' basetable, '' basefield
 from sys.objects O where O.type='V' and O.[schema_id] = 1

 union all

  select 'Field' objecttype,object_name(C.[object_id]) viewname,C.name fieldname,C.column_id,T.name typename,C.max_length,C.precision,C.scale,C.is_identity,
  'what goes here' basetable, 'what goes here' basefield
  from sys.columns C
 left join sys.types T on C.user_type_id=T.system_type_id
 where C.[object_id] in (select O.[object_id] from sys.objects O where O.type='V')
) I
where viewname in ('view1')
order by viewname, column_id

drop view view1
drop table table1
drop table table2
  • 1 1 Answer
  • 0 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-23T15:08:13+00:00Added an answer on May 23, 2026 at 3:08 pm

    There are few tables in the information schema that you can use to deduce the information. A basic query that gives you quite a bit of data will be:

    select * 
    from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE v
        inner join INFORMATION_SCHEMA.COLUMNS v1
            on  v.VIEW_NAME=v1.TABLE_NAME and v.COLUMN_NAME=v1.COLUMN_NAME
    where v.VIEW_NAME='My_View'
    

    The tables in the information schema are:

    select * from INFORMATION_SCHEMA.VIEWS
    select * from INFORMATION_SCHEMA.VIEW_TABLE_USAGE
    select * from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
    

    So try using these.

    However it works only when you use columns directly from the base tabbles without any formula ar deriving.

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

Sidebar

Related Questions

I want to get lambda function that will query for items which were submitted
I want to know number of rows that will be affected by UPDATE query
I want to display the query that is executed in the drupal view. Currently
I have a working query that will return some results(records) from my database, like:
I'm trying to write a query in php that will select a row from
I'm trying to create a query that will return all the jobs published by
I want a query that returns a list of all the (user) stored procedures
I made query that accepts two parameters. Now I want to use that query
i have a query that returns rows that i want, e.g. QuestionID QuestionTitle UpVotes
I have a query that does what i want joining table but i need

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.