SQL Logical errors

  • Hi All,

    I need some one help me identify why am getting logical errors in the output of my query below. Am new to SQL.

    Thanks.

    use MSIU;

    select DISTINCT

    [dbo].[tbl_VOUCHER_ISSUE].dt_VOUCHER_ISSUE_DATE AS IssuanceDate,

    [dbo].[tbl_VOUCHER_ISSUE_DETAIL].str_BARCODE,

    [dbo].[tbl_SALES_TEAM_MASTER].[str_SALES_TEAM_NAME],

    [dbo].[tbl_VOUCHER_CAPTURE_DETAIL].str_BARCODE As VouchersSold,

    [dbo].[tbl_VOUCHER_CAPTURE].dt_VOUCHER_ISSUE_DATE AS SalesDate,

    [dbo].[tbl_SALES_EXECUTIVE_MASTER].str_SALES_EXECUTIVE_NAME AS BCCName,

    [dbo].[tbl_DISTRIBUTOR_MASTER].[str_DISTRIBUTOR_NAME] AS CBDName,

    [dbo].[tbl_DISTRICT_MASTER].str_DISTRICT_NAME CBD_District

    FROM

    [dbo].[tbl_VOUCHER_ISSUE]

    Left Join [dbo].[tbl_VOUCHER_ISSUE_DETAIL]

    ON [dbo].[tbl_VOUCHER_ISSUE].int_VOUCHER_ISSUE_ID = [dbo].[tbl_VOUCHER_ISSUE_DETAIL].int_VOUCHER_ISSUE_ID

    Left Join [dbo].[tbl_SALES_TEAM_MASTER]

    ON [dbo].[tbl_VOUCHER_ISSUE].int_SALES_TEAM_ID = [dbo].[tbl_SALES_TEAM_MASTER].int_SALES_TEAM_ID

    Left Join [dbo].[tbl_VOUCHER_CAPTURE_DETAIL]

    ON [dbo].[tbl_VOUCHER_ISSUE_DETAIL].int_VOUCHER_ISSUE_DETAIL_ID = [dbo].[tbl_VOUCHER_CAPTURE_DETAIL].int_VOUCHER_ISSUE_DETAIL_ID

    Left Join [dbo].[tbl_VOUCHER_CAPTURE]

    ON [dbo].[tbl_VOUCHER_CAPTURE_DETAIL].int_VOUCHER_CAPTURE_ID = [dbo].[tbl_VOUCHER_CAPTURE].int_VOUCHER_CAPTURE_ID

    Left Join [dbo].[tbl_SALES_EXECUTIVE_MASTER]

    ON [dbo].[tbl_VOUCHER_CAPTURE].int_SALES_EXECUTIVE_ID = [dbo].[tbl_SALES_EXECUTIVE_MASTER].int_SALES_EXECUTIVE_ID

    Left Join [dbo].[tbl_DISTRIBUTOR_MASTER]

    ON [dbo].[tbl_VOUCHER_CAPTURE].[int_DISTRIBUTOR_ID] = [dbo].[tbl_DISTRIBUTOR_MASTER].[int_DISTRIBUTOR_ID]

    Left Join [dbo].[tbl_DISTRICT_MASTER]

    ON [dbo].[tbl_DISTRIBUTOR_MASTER].int_DISTRICT_ID = [dbo].[tbl_DISTRICT_MASTER].int_DISTRICT_ID

    WHERE

    [dbo].[tbl_VOUCHER_ISSUE_DETAIL].str_BARCODE LIKE 'FPUG%' AND

    [dbo].[tbl_VOUCHER_ISSUE].dt_VOUCHER_ISSUE_DATE between '2012-06-01' and '2012-06-30' AND

    [dbo].[tbl_VOUCHER_ISSUE_DETAIL].int_STATUS !=5

  • Hi,

    Can you describe error details???

  • I have to say this, and I mean it is a nice way.

    That is just horrible naming convention for your database objects.

    Please change all that. truly the worst ever.

    Andrew SQLDBA

  • The formatting of this makes my eyes bleed. Let's start by cleaning this up a little bit so it is easier to read.

    SELECT DISTINCT [dbo].[tbl_VOUCHER_ISSUE].dt_VOUCHER_ISSUE_DATE AS IssuanceDate

    ,[dbo].[tbl_VOUCHER_ISSUE_DETAIL].str_BARCODE

    ,[dbo].[tbl_SALES_TEAM_MASTER].[str_SALES_TEAM_NAME]

    ,[dbo].[tbl_VOUCHER_CAPTURE_DETAIL].str_BARCODE AS VouchersSold

    ,[dbo].[tbl_VOUCHER_CAPTURE].dt_VOUCHER_ISSUE_DATE AS SalesDate

    ,[dbo].[tbl_SALES_EXECUTIVE_MASTER].str_SALES_EXECUTIVE_NAME AS BCCName

    ,[dbo].[tbl_DISTRIBUTOR_MASTER].[str_DISTRIBUTOR_NAME] AS CBDName

    ,[dbo].[tbl_DISTRICT_MASTER].str_DISTRICT_NAME CBD_District

    FROM [dbo].[tbl_VOUCHER_ISSUE]

    LEFT JOIN [dbo].[tbl_VOUCHER_ISSUE_DETAIL] ON [dbo].[tbl_VOUCHER_ISSUE].int_VOUCHER_ISSUE_ID = [dbo].[tbl_VOUCHER_ISSUE_DETAIL].int_VOUCHER_ISSUE_ID

    LEFT JOIN [dbo].[tbl_SALES_TEAM_MASTER] ON [dbo].[tbl_VOUCHER_ISSUE].int_SALES_TEAM_ID = [dbo].[tbl_SALES_TEAM_MASTER].int_SALES_TEAM_ID

    LEFT JOIN [dbo].[tbl_VOUCHER_CAPTURE_DETAIL] ON [dbo].[tbl_VOUCHER_ISSUE_DETAIL].int_VOUCHER_ISSUE_DETAIL_ID = [dbo].[tbl_VOUCHER_CAPTURE_DETAIL].int_VOUCHER_ISSUE_DETAIL_ID

    LEFT JOIN [dbo].[tbl_VOUCHER_CAPTURE] ON [dbo].[tbl_VOUCHER_CAPTURE_DETAIL].int_VOUCHER_CAPTURE_ID = [dbo].[tbl_VOUCHER_CAPTURE].int_VOUCHER_CAPTURE_ID

    LEFT JOIN [dbo].[tbl_SALES_EXECUTIVE_MASTER] ON [dbo].[tbl_VOUCHER_CAPTURE].int_SALES_EXECUTIVE_ID = [dbo].[tbl_SALES_EXECUTIVE_MASTER].int_SALES_EXECUTIVE_ID

    LEFT JOIN [dbo].[tbl_DISTRIBUTOR_MASTER] ON [dbo].[tbl_VOUCHER_CAPTURE].[int_DISTRIBUTOR_ID] = [dbo].[tbl_DISTRIBUTOR_MASTER].[int_DISTRIBUTOR_ID]

    LEFT JOIN [dbo].[tbl_DISTRICT_MASTER] ON [dbo].[tbl_DISTRIBUTOR_MASTER].int_DISTRICT_ID = [dbo].[tbl_DISTRICT_MASTER].int_DISTRICT_ID

    WHERE [dbo].[tbl_VOUCHER_ISSUE_DETAIL].str_BARCODE LIKE 'FPUG%'

    AND [dbo].[tbl_VOUCHER_ISSUE].dt_VOUCHER_ISSUE_DATE BETWEEN '2012-06-01'

    AND '2012-06-30'

    AND [dbo].[tbl_VOUCHER_ISSUE_DETAIL].int_STATUS != 5

    So now to address the issue.

    I need some one help me identify why am getting logical errors in the output of my query below.

    What does that mean? We don't know your system and we can't see your screen nor do we know the business requirements so we can't begin to answer that based on a query.

    I have to agree with Andrew SQLDBA about the naming conventions. However instead of just telling you how bad they are I think it would help to explain what is bad about them. It doesn't help to just hear "that is awful". Constructive criticism goes long way.

    The only real issue with your naming convention is the prefixes. These do not provide any actual benefit and will actually start to drive you nuts. Why tbl_? Of course it is a table. That adds 4 extra keystrokes and makes your code more difficult to read because it is cluttered with noise. Even worse though is the datatype prefixes on your column names. This is an absolutely horrible idea. What happens when you realize at some point that you need to change datatypes for a column? Suddenly either your prefix is misleading or you have to change all the code that touches that column.

    Then if you use table aliases your query will be a lot easier to read and TONS less typing. I removed all the prefixes and used aliases. Look how much cleaner this looks now.

    SELECT DISTINCT vi.VOUCHER_ISSUE_DATE AS IssuanceDate

    ,vid.BARCODE

    ,stm.[SALES_TEAM_NAME]

    ,vcd.BARCODE AS VouchersSold

    ,vc.VOUCHER_ISSUE_DATE AS SalesDate

    ,sem.SALES_EXECUTIVE_NAME AS BCCName

    ,dm.[DISTRIBUTOR_NAME] AS CBDName

    ,dim.DISTRICT_NAME CBD_District

    FROM [dbo].[VOUCHER_ISSUE] vi

    LEFT JOIN [dbo].[VOUCHER_ISSUE_DETAIL] vid ON vi.VOUCHER_ISSUE_ID = vid.VOUCHER_ISSUE_ID

    LEFT JOIN [dbo].[SALES_TEAM_MASTER] stm ON vi.SALES_TEAM_ID = stm.SALES_TEAM_ID

    LEFT JOIN [dbo].[VOUCHER_CAPTURE_DETAIL] vcd ON vid.VOUCHER_ISSUE_DETAIL_ID = vcd.VOUCHER_ISSUE_DETAIL_ID

    LEFT JOIN [dbo].[VOUCHER_CAPTURE] vc ON vcd.VOUCHER_CAPTURE_ID = vc.VOUCHER_CAPTURE_ID

    LEFT JOIN [dbo].[SALES_EXECUTIVE_MASTER] sem ON vc.SALES_EXECUTIVE_ID = sem.SALES_EXECUTIVE_ID

    LEFT JOIN [dbo].[DISTRIBUTOR_MASTER] dm ON vc.[DISTRIBUTOR_ID] = dm.[DISTRIBUTOR_ID]

    LEFT JOIN [dbo].[DISTRICT_MASTER] dim ON dm.DISTRICT_ID = dim.DISTRICT_ID

    WHERE vid.BARCODE LIKE 'FPUG%'

    AND vi.VOUCHER_ISSUE_DATE BETWEEN '2012-06-01' AND '2012-06-30'

    AND vid.STATUS != 5

    Hard to believe that is the same query huh?

    OK so if you can explain what the issue is we can probably help.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Hey Sean

    You are exactly correct. I should have offered some explanation like you did. I like the way you explained why and gave a nice example.

    Andrew SQLDBA

  • Just another reason to use table aliases in your code, Microsoft has deprecrated 3- and 4-part naming conventions in the select list. These will not be supported in a future version (who knows what version is not known by us) of SQL Server.

  • Lynn Pettis (3/28/2013)


    Just another reason to use table aliases in your code, Microsoft has deprecrated 3- and 4-part naming conventions in the select list. These will not be supported in a future version (who knows what version is not known by us) of SQL Server.

    I had no idea it was actually deprecated. It is certainly painful to have to list it over and over so I haven't done it in years. Good to know.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Hi All,

    Thank you both for your responses. My query has a lot of wording because I was dragging the table names from the database tables in SQL Management Studio.

    However, the problem is that when I execute that query, it runs very well but the output has more records than expected.

    The whole idea is described below;

    I have 6 Tables (VI,VID,SE,ST,VCD and DT) which I need to query as described below with their keys;

    VI VID ST SE VCD DT

    id1 id1 id2 ad2 id1 id3

    id2 id2

    id3

    I need output from all the tables left joined to VI since it's my basis of comparison.

    Thank you.

  • Hi,

    I actually get duplicates when I run my query.

  • I think what I need is a tutorial which will teach me on how to join multiple table.

    Most of the tutorials I am getting on the internet do not show how to deal with multiple tables like 5 or more thanks.

  • sundayose (3/28/2013)


    I think what I need is a tutorial which will teach me on how to join multiple table.

    Most of the tutorials I am getting on the internet do not show how to deal with multiple tables like 5 or more thanks.

    If you want help with this you will have to post ddl, sample data and desired results. See the first link in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • How do I generate the DDL for entire my Database?

    Thanks for the responses.

  • sundayose (3/28/2013)


    How do I generate the DDL for entire my Database?

    Thanks for the responses.

    You don't need the entire database, just the few tables in your query.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Below is the DDL for my tables.

    USE [MSIU]

    GO

    /****** Object: Table [dbo].[tbl_DISTRIBUTOR_MASTER] Script Date: 3/28/2013 7:23:14 PM ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[tbl_DISTRIBUTOR_MASTER](

    [int_DISTRIBUTOR_ID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,

    [str_DISTRIBUTOR_CODE] [varchar](50) NULL,

    [str_DISTRIBUTOR_NAME] [varchar](100) NULL,

    [int_ORAGANISATION_TYPE_ID] [int] NULL,

    [str_PROPRIETOR_NAME] [varchar](50) NULL,

    [str_OCCUPATION] [varchar](50) NULL,

    [str_DESIGNATION] [varchar](50) NULL,

    [str_CONTACT_NUMBER] [varchar](50) NULL,

    [str_EMAIL] [varchar](100) NULL,

    [str_ACCEPTABLE_CREDIT_LIMIT] [varchar](50) NULL,

    [dec_ACCEPTED_DISCOUNT] [decimal](18, 2) NULL,

    [str_POSTAL_ADDRESS] [varchar](200) NULL,

    [int_VILLAGE_ID] [bigint] NOT NULL,

    [int_PARISH_ID] [bigint] NOT NULL,

    [int_SUB_COUNTY_ID] [bigint] NOT NULL,

    [int_COUNTY_ID] [bigint] NOT NULL,

    [int_SUB_DISTRICT_ID] [bigint] NOT NULL,

    [int_DISTRICT_ID] [bigint] NOT NULL,

    [int_COUNTRY_ID] [bigint] NOT NULL,

    [bln_ACTIVE] [bit] NULL,

    [bln_ISDELETED] [bit] NOT NULL,

    [str_CREATED_BY] [varchar](50) NULL,

    [dt_CREATED_DATE] [datetime] NULL,

    [str_UPDATED_BY] [varchar](50) NULL,

    [dt_UPDATED_DATE] [datetime] NULL,

    CONSTRAINT [PK_tbl_DISTRIBUTOR_MASTER] PRIMARY KEY CLUSTERED

    (

    [int_DISTRIBUTOR_ID] 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

    SET ANSI_PADDING OFF

    GO

    /****** Object: Table [dbo].[tbl_SALES_EXECUTIVE_MASTER] Script Date: 3/28/2013 7:23:14 PM ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[tbl_SALES_EXECUTIVE_MASTER](

    [int_SALES_EXECUTIVE_ID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,

    [str_SALES_EXECUTIVE_CODE] [varchar](50) NULL,

    [str_SALES_EXECUTIVE_NAME] [varchar](100) NULL,

    [str_EMAIL] [varchar](100) NULL,

    [str_GENDER] [varchar](50) NULL,

    [str_CONTACT_NUMBER] [varchar](50) NULL,

    [int_ACCEPTABLE_CREDIT_LIMIT] [int] NULL,

    [str_AREA_OF_OPERATION] [varchar](50) NULL,

    [int_SALES_TEAM_ID] [bigint] NULL,

    [str_PHYSICAL_ADDRESS] [varchar](200) NULL,

    [bln_ISACTIVE] [bit] NULL,

    [bln_ISDELETED] [bit] NOT NULL,

    [str_CREATED_BY] [varchar](50) NULL,

    [dt_CREATED_DATE] [datetime] NULL,

    [str_UPDATED_BY] [varchar](50) NULL,

    [dt_UPDATED_DATE] [datetime] NULL,

    [dt_BIRTH_DATE] [datetime] NULL,

    CONSTRAINT [PK_tbl_SALSE_EXECUTIVE_MASTER] PRIMARY KEY CLUSTERED

    (

    [int_SALES_EXECUTIVE_ID] 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

    SET ANSI_PADDING OFF

    GO

    /****** Object: Table [dbo].[tbl_SALES_TEAM_MASTER] Script Date: 3/28/2013 7:23:14 PM ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[tbl_SALES_TEAM_MASTER](

    [int_SALES_TEAM_ID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,

    [str_SALES_TEAM_CODE] [varchar](50) NULL,

    [str_SALES_TEAM_NAME] [varchar](100) NULL,

    [str_DRIVER_NAME] [varchar](100) NULL,

    [str_DESCRIPTION] [varchar](1000) NULL,

    [bln_ISACTIVE] [bit] NOT NULL,

    [bln_ISDELETED] [bit] NOT NULL,

    [str_CREATED_BY] [varchar](50) NULL,

    [dt_CREATED_DATE] [datetime] NULL,

    [str_UPDATED_BY] [varchar](50) NULL,

    [dt_UPDATED_DATE] [datetime] NULL,

    CONSTRAINT [PK_tbl_SALES_TEAM_MASTER] PRIMARY KEY CLUSTERED

    (

    [int_SALES_TEAM_ID] 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

    SET ANSI_PADDING OFF

    GO

    /****** Object: Table [dbo].[tbl_VOUCHER_CAPTURE] Script Date: 3/28/2013 7:23:14 PM ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[tbl_VOUCHER_CAPTURE](

    [int_VOUCHER_CAPTURE_ID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,

    [int_SALES_TEAM_ID] [bigint] NULL,

    [int_SALES_EXECUTIVE_ID] [bigint] NULL,

    [int_DISTRIBUTOR_ID] [bigint] NULL,

    [dt_VOUCHER_ISSUE_DATE] [datetime] NULL,

    [str_CREATED_BY] [varchar](50) NULL,

    [dt_CREATED_DATE] [datetime] NULL,

    [str_UPDATED_BY] [varchar](50) NULL,

    [dt_UPDATED_DATE] [datetime] NULL,

    [int_SALES_AMOUNT] [decimal](18, 2) NULL,

    CONSTRAINT [PK_tbl_VOUCHER_CAPTURE] PRIMARY KEY CLUSTERED

    (

    [int_VOUCHER_CAPTURE_ID] 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

    SET ANSI_PADDING OFF

    GO

    /****** Object: Table [dbo].[tbl_VOUCHER_CAPTURE_DETAIL] Script Date: 3/28/2013 7:23:14 PM ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[tbl_VOUCHER_CAPTURE_DETAIL](

    [int_VOUCHER_CAPTURE_DETAIL_ID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,

    [int_VOUCHER_CAPTURE_ID] [bigint] NOT NULL,

    [int_SEQ_NO] [bigint] NULL,

    [int_BARCODE_DETAIL_ID] [bigint] NOT NULL,

    [int_BARCODE_ID] [bigint] NOT NULL,

    [int_VOUCHER_ISSUE_ID] [bigint] NOT NULL,

    [int_VOUCHER_ISSUE_DETAIL_ID] [bigint] NOT NULL,

    [dt_APPLICABLE_FROM] [datetime] NULL,

    [int_VOUCHER_NUMBER] [int] NULL,

    [str_BARCODE] [varchar](15) NULL,

    [int_STATUS] [int] NULL,

    [str_CREATED_BY] [varchar](50) NULL,

    [dt_CREATED_DATE] [datetime] NULL,

    [str_UPDATED_BY] [varchar](50) NULL,

    [dt_UPDATED_DATE] [datetime] NULL,

    [bln_ISDELETE] [bit] NULL,

    [int_PROJECT_TYPE] [int] NULL,

    CONSTRAINT [PK_tbl_VOUCHER_CAPTURE_DETAIL] PRIMARY KEY CLUSTERED

    (

    [int_VOUCHER_CAPTURE_DETAIL_ID] 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

    SET ANSI_PADDING OFF

    GO

    /****** Object: Table [dbo].[tbl_VOUCHER_ISSUE] Script Date: 3/28/2013 7:23:14 PM ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[tbl_VOUCHER_ISSUE](

    [int_VOUCHER_ISSUE_ID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,

    [int_SALES_TEAM_ID] [bigint] NULL,

    [dt_VOUCHER_ISSUE_DATE] [datetime] NULL,

    [str_CREATED_BY] [varchar](50) NULL,

    [dt_CREATED_DATE] [datetime] NULL,

    [str_UPDATED_BY] [varchar](50) NULL,

    [dt_UPDATED_DATE] [datetime] NULL,

    CONSTRAINT [PK_tbl_VOUCHER_ISSUE] PRIMARY KEY CLUSTERED

    (

    [int_VOUCHER_ISSUE_ID] 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

    SET ANSI_PADDING OFF

    GO

    /****** Object: Table [dbo].[tbl_VOUCHER_ISSUE_DETAIL] Script Date: 3/28/2013 7:23:14 PM ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[tbl_VOUCHER_ISSUE_DETAIL](

    [int_VOUCHER_ISSUE_DETAIL_ID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,

    [int_VOUCHER_ISSUE_ID] [bigint] NOT NULL,

    [int_SEQ_NO] [bigint] NULL,

    [int_BARCODE_DETAIL_ID] [bigint] NOT NULL,

    [int_BARCODE_ID] [bigint] NOT NULL,

    [dt_APPLICABLE_FROM] [datetime] NULL,

    [int_VOUCHER_NUMBER] [int] NULL,

    [str_BARCODE] [varchar](15) NULL,

    [int_STATUS] [int] NULL,

    [int_DISTRIBUTOR_ID] [bigint] NOT NULL,

    [str_CREATED_BY] [varchar](50) NULL,

    [dt_CREATED_DATE] [datetime] NULL,

    [str_UPDATED_BY] [varchar](50) NULL,

    [dt_UPDATED_DATE] [datetime] NULL,

    [bln_ISDELETE] [bit] NULL,

    [int_PROJECT_TYPE] [int] NULL,

    CONSTRAINT [PK_tbl_VOUCHER_ISSUE_DETAIL] PRIMARY KEY CLUSTERED

    (

    [int_VOUCHER_ISSUE_DETAIL_ID] 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

    SET ANSI_PADDING OFF

    GO

    ALTER TABLE [dbo].[tbl_DISTRIBUTOR_MASTER] ADD CONSTRAINT [DF_tbl_DISTRIBUTOR_MASTER_int_VILLAGE_ID] DEFAULT ((0)) FOR [int_VILLAGE_ID]

    GO

    ALTER TABLE [dbo].[tbl_DISTRIBUTOR_MASTER] ADD CONSTRAINT [DF_tbl_DISTRIBUTOR_MASTER_int_PARISH_ID] DEFAULT ((0)) FOR [int_PARISH_ID]

    GO

    ALTER TABLE [dbo].[tbl_DISTRIBUTOR_MASTER] ADD CONSTRAINT [DF_tbl_DISTRIBUTOR_MASTER_int_SUB_COUNTY_ID] DEFAULT ((0)) FOR [int_SUB_COUNTY_ID]

    GO

    ALTER TABLE [dbo].[tbl_DISTRIBUTOR_MASTER] ADD CONSTRAINT [DF_tbl_DISTRIBUTOR_MASTER_int_COUNTY_ID] DEFAULT ((0)) FOR [int_COUNTY_ID]

    GO

    ALTER TABLE [dbo].[tbl_DISTRIBUTOR_MASTER] ADD CONSTRAINT [DF_tbl_DISTRIBUTOR_MASTER_int_SUB_DISTRICT_ID] DEFAULT ((0)) FOR [int_SUB_DISTRICT_ID]

    GO

    ALTER TABLE [dbo].[tbl_DISTRIBUTOR_MASTER] ADD CONSTRAINT [DF_tbl_DISTRIBUTOR_MASTER_int_DISTRICT_ID] DEFAULT ((0)) FOR [int_DISTRICT_ID]

    GO

    ALTER TABLE [dbo].[tbl_DISTRIBUTOR_MASTER] ADD CONSTRAINT [DF_tbl_DISTRIBUTOR_MASTER_int_COUNTRY_ID] DEFAULT ((0)) FOR [int_COUNTRY_ID]

    GO

    ALTER TABLE [dbo].[tbl_DISTRIBUTOR_MASTER] ADD CONSTRAINT [DF_tbl_DISTRIBUTOR_MASTER_bln_ACTIVE] DEFAULT ((1)) FOR [bln_ACTIVE]

    GO

    ALTER TABLE [dbo].[tbl_DISTRIBUTOR_MASTER] ADD CONSTRAINT [DF_tbl_DISTRIBUTOR_MASTER_bln_ISDELETED] DEFAULT ((0)) FOR [bln_ISDELETED]

    GO

    ALTER TABLE [dbo].[tbl_SALES_EXECUTIVE_MASTER] ADD CONSTRAINT [DF_tbl_SALSE_EXECUTIVE_MASTER_bln_ISACTIVE] DEFAULT ((1)) FOR [bln_ISACTIVE]

    GO

    ALTER TABLE [dbo].[tbl_SALES_EXECUTIVE_MASTER] ADD CONSTRAINT [DF_tbl_SALSE_EXECUTIVE_MASTER_bln_ISDELETED] DEFAULT ((0)) FOR [bln_ISDELETED]

    GO

    ALTER TABLE [dbo].[tbl_SALES_TEAM_MASTER] ADD CONSTRAINT [DF_tbl_SALES_TEAM_MASTER_bln_ISDELETED1] DEFAULT ((1)) FOR [bln_ISACTIVE]

    GO

    ALTER TABLE [dbo].[tbl_SALES_TEAM_MASTER] ADD CONSTRAINT [DF_tbl_SALES_TEAM_MASTER_bln_ISDELETED] DEFAULT ((0)) FOR [bln_ISDELETED]

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_CAPTURE_DETAIL] ADD CONSTRAINT [DF_tbl_VOUCHER_CAPTURE_DETAIL_bln_ISDELETE] DEFAULT ((0)) FOR [bln_ISDELETE]

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_ISSUE_DETAIL] ADD CONSTRAINT [DF_Table_1_int_CLIENT_ID] DEFAULT ((0)) FOR [int_DISTRIBUTOR_ID]

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_ISSUE_DETAIL] ADD CONSTRAINT [DF_tbl_VOUCHER_ISSUE_DETAIL_bln_ISDELETE] DEFAULT ((0)) FOR [bln_ISDELETE]

    GO

    ALTER TABLE [dbo].[tbl_DISTRIBUTOR_MASTER] WITH CHECK ADD CONSTRAINT [FK_tbl_DISTRIBUTOR_MASTER_tbl_VILLAGE_MASTER] FOREIGN KEY([int_VILLAGE_ID])

    REFERENCES [dbo].[tbl_VILLAGE_MASTER] ([int_VILLAGE_ID])

    GO

    ALTER TABLE [dbo].[tbl_DISTRIBUTOR_MASTER] CHECK CONSTRAINT [FK_tbl_DISTRIBUTOR_MASTER_tbl_VILLAGE_MASTER]

    GO

    ALTER TABLE [dbo].[tbl_SALES_EXECUTIVE_MASTER] WITH CHECK ADD CONSTRAINT [FK_tbl_SALES_EXECUTIVE_MASTER_tbl_SALES_TEAM_MASTER] FOREIGN KEY([int_SALES_TEAM_ID])

    REFERENCES [dbo].[tbl_SALES_TEAM_MASTER] ([int_SALES_TEAM_ID])

    GO

    ALTER TABLE [dbo].[tbl_SALES_EXECUTIVE_MASTER] CHECK CONSTRAINT [FK_tbl_SALES_EXECUTIVE_MASTER_tbl_SALES_TEAM_MASTER]

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_CAPTURE] WITH CHECK ADD CONSTRAINT [FK_tbl_VOUCHER_CAPTURE_tbl_DISTRIBUTOR_MASTER] FOREIGN KEY([int_DISTRIBUTOR_ID])

    REFERENCES [dbo].[tbl_DISTRIBUTOR_MASTER] ([int_DISTRIBUTOR_ID])

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_CAPTURE] CHECK CONSTRAINT [FK_tbl_VOUCHER_CAPTURE_tbl_DISTRIBUTOR_MASTER]

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_CAPTURE] WITH CHECK ADD CONSTRAINT [FK_tbl_VOUCHER_CAPTURE_tbl_SALES_EXECUTIVE_MASTER] FOREIGN KEY([int_SALES_EXECUTIVE_ID])

    REFERENCES [dbo].[tbl_SALES_EXECUTIVE_MASTER] ([int_SALES_EXECUTIVE_ID])

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_CAPTURE] CHECK CONSTRAINT [FK_tbl_VOUCHER_CAPTURE_tbl_SALES_EXECUTIVE_MASTER]

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_CAPTURE] WITH CHECK ADD CONSTRAINT [FK_tbl_VOUCHER_CAPTURE_tbl_SALES_TEAM_MASTER] FOREIGN KEY([int_SALES_TEAM_ID])

    REFERENCES [dbo].[tbl_SALES_TEAM_MASTER] ([int_SALES_TEAM_ID])

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_CAPTURE] CHECK CONSTRAINT [FK_tbl_VOUCHER_CAPTURE_tbl_SALES_TEAM_MASTER]

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_ISSUE] WITH CHECK ADD CONSTRAINT [FK_tbl_VOUCHER_ISSUE_tbl_SALES_TEAM_MASTER] FOREIGN KEY([int_SALES_TEAM_ID])

    REFERENCES [dbo].[tbl_SALES_TEAM_MASTER] ([int_SALES_TEAM_ID])

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_ISSUE] CHECK CONSTRAINT [FK_tbl_VOUCHER_ISSUE_tbl_SALES_TEAM_MASTER]

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_ISSUE_DETAIL] WITH CHECK ADD CONSTRAINT [FK_tbl_VOUCHER_ISSUE_DETAIL_tbl_BARCODE_DETAIL] FOREIGN KEY([int_BARCODE_DETAIL_ID])

    REFERENCES [dbo].[tbl_BARCODE_DETAIL] ([int_BARCODE_DETAIL_ID])

    GO

    ALTER TABLE [dbo].[tbl_VOUCHER_ISSUE_DETAIL] CHECK CONSTRAINT [FK_tbl_VOUCHER_ISSUE_DETAIL_tbl_BARCODE_DETAIL]

    GO

  • That was pretty close. There are a few foreign keys to tables we don't have but that is workable. We are still missing tbl_DISTRICT_MASTER. And more importantly we still don't have any data to work with.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic. Login to reply