tags:

views:

85

answers:

5

Hi.

I have two tables test1 { id, name } and test2 { id, name, family } and i write this query:

SELECT dbo.test1.*, dbo.test2.*
FROM dbo.test1
CROSS JOIN dbo.test2

but in datagridview1, I want to show the fields (on header) like this:

test1.id test1.name test2.id test2.name test2.family, except this

id name id name family

which changes need for my query.

Thanks.

+3  A: 

The short answer is: you can change the caption of the grid columns as you like. You can also reorder/hide/sort columns as you like.

Scoregraphic
+1  A: 

You'll need to select the columns individually, and use the as keyword:

SELECT dbo.test1.id as test1id, dbo.test2.id as test2id ...
FROM dbo.test1
CROSS JOIN dbo.test2
Douglas
the `as` keyword is not required, but may make things more readable
Scoregraphic
or you can use the dot with square brackets I think, `dbo.test1.id as [test1.id]`. But you'd be better off selecting and renaming the columns in your data grid control.
Rup
thanks for your attention.if i have more tables that i want to cross join them and i want to show each tabale with fileds like this: tb1.filed1 tb1.filed2 tb2.filed1 ..... should i do?
na.fa
i mean how can i select fileds with details like it's tabale's name.
na.fa
+3  A: 

I would write the select like:

select t1.id as "test1.Id", 
       t1.name as "test1.Name", 
       t2.id as "test2.Id", 
       t2.name as "test2.Name", 
       t2.family as "Test2.Family" 
from test1 t1, test2 t2

But with that query you will get a cartesian product, if you don't add a proper Where Clause.

BitKFu
+1  A: 

Are you asking how to alias the columns?

SELECT
    t1.id AS [test1.id],
    t1.name AS [test1.name] ,
    t2.id AS [test2.id], 
    t2.name AS [test2.name] ,
    t2.family AS [test2.family]
FROM dbo.test1 t1
CROSS JOIN dbo.test2 t2

As the names you want don't meet the standard rules for identifiers they need to be quoted.

Martin Smith
+1  A: 

bad code is bad. however I would do it as @Scoregraphic mention; even if all ur columns come back as "co1; col1; col1" u can change the order / labeling of each in the properties for the columns in the datagridview

Dovix