I Have 3 table Like this:
Table 1:
Name = Tbl_Mian_code
| M_Code | Master_Code | Master_Name | Main_Type | Main_Gruop | Main_Txt | |
| 1001 | 1001 | Bank | 01 | 01 | ||
| 1002 | 1002 | Cash | 01 | 02 | ||
| 1003 | 1003 | Good's | 02 | 01 | ||
Table 2:
Name = Tbl_Mian_code
| Id_2 | Main_Code | Sub_Code | Master_Code | Master_Name | Sub_Txt |
| 1 | 1001 | 0001 | 1001 0001 | Bank_Visa | |
| 4 | 1001 | 0002 | 1001 0002 | Bank_234 | |
| 5 | 1002 | 0001 | 1002 0001 | Cash_1 | |
| 6 | 1002 | 0002 | 1002 0002 | Cash_2 | |
| 7 | 1003 | 0001 | 1003 0001 | Nokia | |
| 9 | 1003 | 0002 | 1003 0002 | Samsung |
Table 3:
Name = Tbl_Sub_T
| Id_3 | Sub_Master Code | Sub_T_Code | Master_Code | Master_Name | Sub_T_Txt |
| 1 | 10030001 | 0001 | 1003 00010001 | Nokia 6600 | |
| 2 | 10030001 | 0002 | 1003 00010002 | Nokia 6630 | |
| 3 | 10030001 | 0003 | 1003 00010003 | Nokia 6230 | |
| 4 | 10030002 | 0001 | 1003 00020001 | Samsung T 60 | |
| 5 | 10030002 | 0002 | 1003 00020002 | Samsung E500 | |
| 6 | 10030002 | 0003 | 1003 00020003 | Samsung E700 |
I want make table 4 like this one in SQL server 2005 or 2000
| Master_Code | Master_Name |
| 1001 | Bank |
| 1001 0001 | Bank_Visa |
| 1001 0002 | Bank_234 |
| 1002 | Cash |
| 1002 0001 | Cash_1 |
| 1002 0002 | Cash_2 |
| 1003 | Good's |
| 1003 0001 | Nokia |
| 1003 00010001 | Nokia 6600 |
| 1003 00010002 | Nokia 6630 |
| 1003 00010003 | Nokia 6230 |
| 1003 0002 | Samsung |
| 1003 00020001 | Samsung T 60 |
| 1003 00020002 | Samsung E500 |
| 1003 00020003 | Samsung E700 |
The sub code is unique but depends on main code. As you see master Code is unique.
How I can generate it by function in SQL.
And How to make table 4
Say me how.
Tanks For your help.
Make table 1, 2, 3 by this code & help me to make table 4 with your code.if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_Tbl_Sub_Code_Tbl_Mian_code]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE [dbo].[Tbl_Sub_Code] DROP CONSTRAINT FK_Tbl_Sub_Code_Tbl_Mian_code
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Tbl_Sub_Code]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Tbl_Sub_Code]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Tbl_Mian_code]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Tbl_Mian_code]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Tbl_Sub_T]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Tbl_Sub_T]
GO
CREATE TABLE [dbo].[Tbl_Mian_code] (
[Main_Code] [char] (4) COLLATE Arabic_CI_AS NOT NULL ,
[Master_Code] AS ([Main_code]) ,
[Name] [nvarchar] (255) COLLATE Arabic_CI_AS NOT NULL ,
[Main_Type] [char] (2) COLLATE Arabic_CI_AS NULL ,
[Main_Gruop] [char] (2) COLLATE Arabic_CI_AS NULL ,
[Main_Txt] [text] COLLATE Arabic_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[Tbl_Sub_T] (
[Id_3] [int] NOT NULL ,
[Sub_Master_Code] [char] (8) COLLATE Arabic_CI_AS NOT NULL ,
[Sub_T_Code] [char] (4) COLLATE Arabic_CI_AS NOT NULL ,
[Master_Code] AS ([sub_master_code] + [sub_T_code]) ,
[Master_Name] [nvarchar] (255) COLLATE Arabic_CI_AS NOT NULL ,
[Sub_T_Txt] [ntext] COLLATE Arabic_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[Tbl_Sub_Code] (
[Id_2] [int] IDENTITY (1, 1) NOT NULL ,
[Main_Code] [char] (4) COLLATE Arabic_CI_AS NOT NULL ,
[Sub_Code] [char] (4) COLLATE Arabic_CI_AS NOT NULL ,
[Master_Code] AS ([main_code] + [sub_code]) ,
[Master_Name] [nvarchar] (255) COLLATE Arabic_CI_AS NOT NULL ,
[Sub_Txt] [ntext] COLLATE Arabic_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Tbl_Mian_code] WITH NOCHECK ADD
CONSTRAINT [PK_Tbl_Mian_code] PRIMARY KEY CLUSTERED
(
[Main_Code]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Tbl_Sub_T] WITH NOCHECK ADD
CONSTRAINT [PK_Tbl_Sub_T] PRIMARY KEY CLUSTERED
(
[Id_3]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Tbl_Sub_Code] WITH NOCHECK ADD
CONSTRAINT [PK_Tbl_Sub_Code] PRIMARY KEY CLUSTERED
(
[Id_2]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Tbl_Sub_Code] WITH NOCHECK ADD
CONSTRAINT [IX_Tbl_Sub_Code] UNIQUE NONCLUSTERED
(
[Master_Code]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Tbl_Sub_Code] ADD
CONSTRAINT [FK_Tbl_Sub_Code_Tbl_Mian_code] FOREIGN KEY
(
[Main_Code]
) REFERENCES [dbo].[Tbl_Mian_code] (
[Main_Code]
)
GO

I want make table 4 like this one in SQL server 2005
diego jaramillo
WOW It's realy works.
I cant belive it .
thanks . thanks For All.
I got my Answer . And I am very happy.
Group By Expressions
given that sql is set to the above select statement and aCon is an initialized SQLConnection:
Dim da as SQLDataAdapter = new SQLDataAdapter(sql, aCon)
dim ds as DataSet = new DataSet()
da.Fill(da)
tgallagher
David Deen
SmartDumb
Hi
I use this code to make view
create view My_Code as
select [master_code], [master_name]
from
(
select [master_code], [master_name],
cast([master_code] as int) [master_sort],
0 [detail_sort],
0 [sub_detail_sort] from [Tbl_Mian_code]
union
select [detail].[master_code], [detail].[master_name],
cast([master].[master_code] as int),
cast([detail].[sub_code] as int),
0
from [Tbl_Mian_code] [master] inner join [Tbl_sub_code] [detail]
on [master].[master_code] = [detail].[main_code]
union
select [subdetail].[master_code], [subdetail].[master_name],
cast([master].[master_code] as int),
cast([detail].[sub_code] as int),
cast([subdetail].[sub_t_code] as int)
from (Tbl_Mian_code [master] inner join [Tbl_sub_code] [detail]
on [master].[master_code] = [detail].[main_code]) inner join [Tbl_sub_t] [subdetail]
on [detail].[Master_Code] = [subdetail].[Sub_Master_Code]
)temp
order by [master_sort], [detail_sort], [sub_detail_sort]
and I got this Erorr
Server: Msg 1033, Level 15, State 1, Procedure MyViewName, Line 26
The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified.
hvymtl
Tnanks for your answer.
But let me ask in other way .
how I can save code you write as a view
I khow how to load a view or table in windows form.
but I can't save your code as View!!!
Ken Morley
Hi
Thanks for your answer.
If it is not possble in SQL server 2005 what I Can do in SQL server 2005
what I can do In SQL server 2000
I Can chenge My program to Use SQL server 2000. but I need a view that showed in my frist post.
How I can make it . I need that table . the way of making it is not major.
by the way . I need to use that table very much in my program . ( may be 1 time evry min ) but my table 1 . 2 . 3 dont change very much. ( once when a Good's add or . . . )
the performance is my main problem too.
Ian Bavey - MSFT
I want to say:
'Way To Go!!! Including the script to create the tables you are working with!!!'
Thats the way to do it if you really want help!
cheers and good luck!
davide_c
[enter select sql here]
great examples in the help files
PatGO
QingY
create view My_Code as
select TOP 100 PERCENT [master_code], [master_name]
from
(
select [master_code], [master_name],
cast([master_code] as int) [master_sort],
0 [detail_sort],
0 [sub_detail_sort] from [Tbl_Mian_code]
union
select [detail].[master_code], [detail].[master_name],
cast([master].[master_code] as int),
cast([detail].[sub_code] as int),
0
from [Tbl_Mian_code] [master] inner join [Tbl_sub_code] [detail]
on [master].[master_code] = [detail].[main_code]
union
select [subdetail].[master_code], [subdetail].[master_name],
cast([master].[master_code] as int),
cast([detail].[sub_code] as int),
cast([subdetail].[sub_t_code] as int)
from (Tbl_Mian_code [master] inner join [Tbl_sub_code] [detail]
on [master].[master_code] = [detail].[main_code]) inner join [Tbl_sub_t] [subdetail]
on [detail].[Master_Code] = [subdetail].[Sub_Master_Code]
)temp
order by [master_sort], [detail_sort], [sub_detail_sort]
gamesplant
looks like a simple union view. . .
caveat emptor (not checked, just hacked)!!!
select [master_code], [master_name]
from
(
select [master_code], [master_name],
cast([master_code] as int) [master_sort],
0 [detail_sort],
0 [sub_detail_sort] from [Tbl_Mian_code]
union
select [detail].[master_code], [detail].[master_name],
cast([master].[master_code] as int),
cast([detail].[sub_code] as int),
0
from [Tbl_Mian_code] [master] inner join [Tbl_sub_code] [detail]
on [master].[master_code] = [detail].[main_code]
union
select [subdetail].[master_code], [subdetail].[master_name],
cast([master].[master_code] as int),
cast([detail].[sub_code] as int),
cast([subdetail].[sub_t_code] as int)
from (Tbl_Mian_code [master] inner join [Tbl_sub_code] [detail]
on [master].[master_code] = [detail].[main_code]) inner join [Tbl_sub_t] [subdetail]
on [detail].[Master_Code] = [subdetail].[Sub_Master_Code]
)temp
order by [master_sort], [detail_sort], [sub_detail_sort]