Need To display data in Blocks (Column name: value )

  • Hi All,

    I want to display data in block. Below is the Create,Insert Script and format of desired output. Please help.

    CREATE TABLE [dbo].[Test_AA](

    [CounterpartID] [varchar](2000) NULL,

    [CounterpartName] [varchar](2000) NULL,

    [SATName] [varchar](2000) NULL,

    [NameOfBO] [varchar](2000) NULL,

    [SATAccountNumber] [varchar](2000) NULL,

    [SATCode] [varchar](2000) NULL,

    [CurrentOperator] [varchar](2000) NULL,

    [FatherName] [varchar](2000) NULL,

    [DateOfBirth] [varchar](2000) NULL,

    [TypeOfIdentification] [varchar](2000) NULL,

    [IdenficationNumber] [varchar](2000) NULL,

    [IssueDate] [varchar](2000) NULL,

    [IssueAuthority] [varchar](2000) NULL,

    [AddressStreet] [varchar](2000) NULL,

    [AddressStreetNumber] [varchar](2000) NULL,

    [AddressPostalCode] [varchar](2000) NULL,

    [AddressCity] [varchar](2000) NULL,

    [AddressCountry] [varchar](2000) NULL,

    [Nationality] [varchar](2000) NULL,

    [CountryOfTaxDomiciled] [varchar](2000) NULL,

    [IncorporationStatus] [varchar](2000) NULL,

    [GreekTaxIDNumber] [varchar](2000) NULL,

    [GreekTaxOffice] [varchar](2000) NULL,

    [GreekCompanyRegNumber] [varchar](2000) NULL,

    [CompanyIssueAuthority] [varchar](2000) NULL,

    [CompanyIssueDate] [varchar](2000) NULL,

    [Country] [varchar](2000) NULL,

    [Joint Account] [varchar](3) NOT NULL

    GO

    INSERT [dbo].[Teast_AA]

    ( [ID], [Name], [SATName], [NameOfBO], [SATAccountNumber],

    [SATCode], [CurrentOperator], [FatherName], [DateOfBirth],

    [TypeOfIdentification], [IdenficationNumber], [IssueDate],

    [IssueAuthority], [AddressStreet], [AddressStreetNumber],

    [AddressPostalCode], [AddressCity], [AddressCountry],

    [Nationality], [CountryOfTaxDomiciled],

    [IncorporationStatus], [GreekTaxIDNumber], [GreekTaxOffice],

    [GreekCompanyRegNumber], [CompanyIssueAuthority],

    [CompanyIssueDate], [Country], [JointAccount] )

    VALUES ( N'17388', N'Scottie Berwick', N'Scottie Berwick', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'Yes' )

    , ( N'', N'', N'', N'NAMe1', N'', N'', N'', N'FNAME1',

    N'07/26/2013', N'passport', N'PA1', N'07/28/2013', N'PA2',

    N'', N'', N'', N'', N'', N'PA4', N'Pakistan', N'', N'', N'',

    N'', N'', N'', N'', N'Yes' )

    , ( N'', N'', N'', N'NAMe2', N'', N'', N'', N'FNAME2',

    N'07/28/2013', N'driverslicense', N'DA1', N'07/29/2013',

    N'DA2', N'', N'', N'', N'', N'', N'DA4', N'Åland Islands',

    N'', N'', N'', N'', N'', N'', N'', N'Yes' )

    , ( N'2457', N'Shawnta Balzarini', N'Shawnta Balzarini', N'',

    N'account_number_l1_test', N'account_code_l1_test',

    N'account_operator_l1_test', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'Yes' )

    , ( N'', N'', N'account_name_diff_bo_l2_test',

    N'account_name_l2_test', N'account_number_l2_test',

    N'account_code_l2_test', N'', N'account_father_name_l2_test',

    N'date_account_birth_l2_test', N'ca_account_id_type_l2_test',

    N'account_id_number_l2_test',

    N'date_account_doc_issue_l2_test',

    N'account_doc_issue_authority_l2_test', N'', N'', N'', N'',

    N'', N'account_nationality_l2_test',

    N'country_account_l2_test', N'account_inc_status_l2_test',

    N'ca_greek_tax_afm_l2_test', N'ca_greek_tax_doy_l2_test',

    N'ca_company_armae_l2_test',

    N'ca_company_issue_authority_l2_test',

    N'date_ca_company_issue_l2_test',

    N'country_ca_company_l2_test', N'Yes' )

    , ( N'', N'', N'account_name_diff_bo_l3_test',

    N'account_name_l3_test', N'account_number_l3_test',

    N'account_code_l3_test', N'', N'account_father_name_l3_test',

    N'date_account_birth_l3_test', N'ca_account_id_type_l3_test',

    N'account_id_number_l3_test',

    N'date_account_doc_issue_l3_test',

    N'account_doc_issue_authority_l3_test', N'', N'', N'', N'',

    N'', N'account_nationality_l3_test',

    N'country_account_l3_test', N'account_inc_status_l3_test',

    N'ca_greek_tax_afm_l3_test', N'ca_greek_tax_doy_l3_test',

    N'ca_company_armae_l3_test',

    N'ca_company_issue_authority_l3_test',

    N'date_ca_company_issue_l3_test',

    N'country_ca_company_l3_test', N'Yes' )

    , ( N'', N'', N'account_name_diff_bo_l4_test',

    N'account_name_l4_test', N'account_number_l4_test',

    N'account_code_l4_test', N'', N'account_father_name_l4_test',

    N'date_account_birth_l4_test', N'ca_account_id_type_l4_test',

    N'account_id_number_l4_test',

    N'date_account_doc_issue_l4_test',

    N'account_doc_issue_authority_l4_test', N'', N'', N'', N'',

    N'', N'account_nationality_l4_test',

    N'country_account_l4_test', N'account_inc_status_l4_test',

    N'ca_greek_tax_afm_l4_test', N'ca_greek_tax_doy_l4_test',

    N'ca_company_armae_l4_test',

    N'ca_company_issue_authority_l4_test',

    N'date_ca_company_issue_l4_test',

    N'country_ca_company_l4_test', N'Yes' )

    , ( N'120132', N'Priscilla Andrada', N'account_name_test',

    N'account_name_test', N'account_number_test',

    N'account_code_test', N'account_operator_test',

    N'account_father_name_test', N'date_account_birth_test',

    N'ca_account_id_type_test', N'account_id_number_test',

    N'date_account_doc_issue_test',

    N'account_doc_issue_authority_test', N'', N'', N'', N'', N'',

    N'account_nationality_test', N'country_account_test',

    N'account_inc_status_test', N'ca_greek_tax_afm_test',

    N'ca_greek_tax_doy_test', N'ca_company_armae_test',

    N'ca_company_issue_authority_test',

    N'date_ca_company_issue_test', N'country_ca_company_test',

    N'No' )

    , ( N'682472', N'Sacha Blasenhauer', N'ggg', N'ggg',

    N'4444444444', N'44443423444', N'test', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'No' )

    , ( N'682472', N'Sacha Blasenhauer', N'2222', N'2222',

    N'2222222222', N'2222222222', N'22222', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'No' )

    , ( N'17388', N'Scottie Berwick', N'Scottie Berwick', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'Yes' )

    , ( N'', N'', N'', N'FNAME1', N'', N'', N'', N'FAN1',

    N'08/29/2013', N'DriversLicense', N'DI1', N'08/28/2013',

    N'IA1', N'S1', N'N1', N'PO1', N'CI1', N'Co1', N'NA1',

    N'Afghanistan', N'SA1', N'AFM1', N'DOY1', N'', N'', N'', N'',

    N'Yes' )

    , ( N'', N'', N'', N'FNAME2', N'', N'', N'', N'FATNAME2',

    N'08/01/2013', N'ID', N'ID2', N'08/02/2013', N'IA2', N'S2',

    N'N2', N'PO2', N'CI2', N'CO2', N'NA2', N'Afghanistan',

    N'SA2', N'', N'', N'', N'', N'', N'', N'Yes' )

    , ( N'', N'', N'', N'FNAME3', N'', N'', N'', N'FATNAME3',

    N'08/15/2013', N'Passport', N'PA3', N'08/16/2013', N'IA3',

    N'S3', N'N3', N'PO3', N'CI3', N'CO3', N'NA3',

    N'Åland Islands', N'SA3', N'', N'', N'AREM3', N'IAA3',

    N'08/17/2013', N'Albania', N'Yes' )

    , ( N'682472', N'Sacha Blasenhauer', N'abcd', N'abcd',

    N'111111111111', N'1111111111', N'11111111111', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'No' )

    , ( N'682472', N'Sacha Blasenhauer', N'', N'', N'', N'', N'',

    N'asd', N'09/01/2013', N'Passport', N'sdf', N'09/02/2013',

    N'sdf', N'sdf', N'sdf', N'dsf', N'sdf', N'sdf', N'sdf',

    N'Australia', N'', N'', N'', N'eee', N'eee', N'09/29/2013',

    N'Albania', N'No' )

    , ( N'17388', N'Scottie Berwick', N'Scottie Berwick', N'',

    N'7845124578', N'7845121212', N'sdfsd', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'Yes' )

    , ( N'', N'', N'Test', N'Test', N'1234567897', N'0000000000',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'Yes' )

    , ( N'', N'', N'SAT2', N'SAT2', N'4567891231', N'4545454545',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'Yes' )

    , ( N'17388', N'Scottie Berwick', N'Scottie Berwick', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'Yes' )

    GO

    Result Should be like This :

    ID:17388

    Name:Scottie Berwick

    SATName:Scottie Berwick

    NameOfBO:

    SATAccountNumber:

    SATCode:

    CurrentOperator:

    FatherName:

    DateOfBirth:

    TypeOfIdentification:

    IdenficationNumber:

    IssueDate:

    IssueAuthority:

    AddressStreet:

    AddressStreetNumber:

    AddressPostalCode:

    AddressCity:

    AddressCountry:

    Nationality:

    CountryOfTaxDomiciled:

    IncorporationStatus:

    GreekTaxIDNumber:

    GreekTaxOffice:

    GreekCompanyRegNumber:

    CompanyIssueAuthority:

    CompanyIssueDate:

    Country:

    JointAccount:Yes

    Thanks

  • I will not argue around your needs. Check in BOL for the UNPIVOT operator or APPLY.

    SET NOCOUNT ON;

    USE tempdb;

    GO

    DECLARE @T TABLE (

    c1 int NOT NULL PRIMARY KEY,

    c2 varchar(25) NOT NULL

    );

    INSERT INTO @T (c1, c2)

    VALUES (1, 'uno'), (2, 'dos');

    SELECT

    c1 AS rid,

    c3 + ': ' + c4 AS whatever

    FROM

    @T

    CROSS APPLY

    (

    SELECT c3, c4 FROM (VALUES ('c1', LTRIM(c1)), ('c2', c2)) AS S(c3, c4)

    ) AS R;

    GO

  • There were some problems with your sample data which I cleaned up.

    USE tempdb

    GO

    IF OBJECT_ID('tempdb.dbo.Test_AA') IS NOT NULL DROP TABLE dbo.Test_AA

    IF OBJECT_ID('tempdb..#Test_AA_stg') IS NOT NULL DROP TABLE #Test_AA_stg

    GO

    CREATE TABLE [dbo].[Test_AA](

    ID int not null ,

    Name varchar(100) not null,

    [CounterpartID] [varchar](2000) NULL,

    [CounterpartName] [varchar](2000) NULL,

    [SATName] [varchar](2000) NULL,

    [NameOfBO] [varchar](2000) NULL,

    [SATAccountNumber] [varchar](2000) NULL,

    [SATCode] [varchar](2000) NULL,

    [CurrentOperator] [varchar](2000) NULL,

    [FatherName] [varchar](2000) NULL,

    [DateOfBirth] [varchar](2000) NULL,

    [TypeOfIdentification] [varchar](2000) NULL,

    [IdenficationNumber] [varchar](2000) NULL,

    [IssueDate] [varchar](2000) NULL,

    [IssueAuthority] [varchar](2000) NULL,

    [AddressStreet] [varchar](2000) NULL,

    [AddressStreetNumber] [varchar](2000) NULL,

    [AddressPostalCode] [varchar](2000) NULL,

    [AddressCity] [varchar](2000) NULL,

    [AddressCountry] [varchar](2000) NULL,

    [Nationality] [varchar](2000) NULL,

    [CountryOfTaxDomiciled] [varchar](2000) NULL,

    [IncorporationStatus] [varchar](2000) NULL,

    [GreekTaxIDNumber] [varchar](2000) NULL,

    [GreekTaxOffice] [varchar](2000) NULL,

    [GreekCompanyRegNumber] [varchar](2000) NULL,

    [CompanyIssueAuthority] [varchar](2000) NULL,

    [CompanyIssueDate] [varchar](2000) NULL,

    [Country] [varchar](2000) NULL,

    [Joint Account] [varchar](3) NOT NULL)

    GO

    INSERT [dbo].[Test_AA]

    ( [ID], [Name], [SATName], [NameOfBO], [SATAccountNumber],

    [SATCode], [CurrentOperator], [FatherName], [DateOfBirth],

    [TypeOfIdentification], [IdenficationNumber], [IssueDate],

    [IssueAuthority], [AddressStreet], [AddressStreetNumber],

    [AddressPostalCode], [AddressCity], [AddressCountry],

    [Nationality], [CountryOfTaxDomiciled],

    [IncorporationStatus], [GreekTaxIDNumber], [GreekTaxOffice],

    [GreekCompanyRegNumber], [CompanyIssueAuthority],

    [CompanyIssueDate], [Country], [Joint Account])

    VALUES ( N'17388', N'Scottie Berwick', N'Scottie Berwick', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'Yes' )

    , ( N'', N'', N'', N'NAMe1', N'', N'', N'', N'FNAME1',

    N'07/26/2013', N'passport', N'PA1', N'07/28/2013', N'PA2',

    N'', N'', N'', N'', N'', N'PA4', N'Pakistan', N'', N'', N'',

    N'', N'', N'', N'', N'Yes' )

    , ( N'', N'', N'', N'NAMe2', N'', N'', N'', N'FNAME2',

    N'07/28/2013', N'driverslicense', N'DA1', N'07/29/2013',

    N'DA2', N'', N'', N'', N'', N'', N'DA4', N'Åland Islands',

    N'', N'', N'', N'', N'', N'', N'', N'Yes' )

    , ( N'2457', N'Shawnta Balzarini', N'Shawnta Balzarini', N'',

    N'account_number_l1_test', N'account_code_l1_test',

    N'account_operator_l1_test', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'Yes' )

    , ( N'', N'', N'account_name_diff_bo_l2_test',

    N'account_name_l2_test', N'account_number_l2_test',

    N'account_code_l2_test', N'', N'account_father_name_l2_test',

    N'date_account_birth_l2_test', N'ca_account_id_type_l2_test',

    N'account_id_number_l2_test',

    N'date_account_doc_issue_l2_test',

    N'account_doc_issue_authority_l2_test', N'', N'', N'', N'',

    N'', N'account_nationality_l2_test',

    N'country_account_l2_test', N'account_inc_status_l2_test',

    N'ca_greek_tax_afm_l2_test', N'ca_greek_tax_doy_l2_test',

    N'ca_company_armae_l2_test',

    N'ca_company_issue_authority_l2_test',

    N'date_ca_company_issue_l2_test',

    N'country_ca_company_l2_test', N'Yes' )

    , ( N'', N'', N'account_name_diff_bo_l3_test',

    N'account_name_l3_test', N'account_number_l3_test',

    N'account_code_l3_test', N'', N'account_father_name_l3_test',

    N'date_account_birth_l3_test', N'ca_account_id_type_l3_test',

    N'account_id_number_l3_test',

    N'date_account_doc_issue_l3_test',

    N'account_doc_issue_authority_l3_test', N'', N'', N'', N'',

    N'', N'account_nationality_l3_test',

    N'country_account_l3_test', N'account_inc_status_l3_test',

    N'ca_greek_tax_afm_l3_test', N'ca_greek_tax_doy_l3_test',

    N'ca_company_armae_l3_test',

    N'ca_company_issue_authority_l3_test',

    N'date_ca_company_issue_l3_test',

    N'country_ca_company_l3_test', N'Yes' )

    , ( N'', N'', N'account_name_diff_bo_l4_test',

    N'account_name_l4_test', N'account_number_l4_test',

    N'account_code_l4_test', N'', N'account_father_name_l4_test',

    N'date_account_birth_l4_test', N'ca_account_id_type_l4_test',

    N'account_id_number_l4_test',

    N'date_account_doc_issue_l4_test',

    N'account_doc_issue_authority_l4_test', N'', N'', N'', N'',

    N'', N'account_nationality_l4_test',

    N'country_account_l4_test', N'account_inc_status_l4_test',

    N'ca_greek_tax_afm_l4_test', N'ca_greek_tax_doy_l4_test',

    N'ca_company_armae_l4_test',

    N'ca_company_issue_authority_l4_test',

    N'date_ca_company_issue_l4_test',

    N'country_ca_company_l4_test', N'Yes' )

    , ( N'120132', N'Priscilla Andrada', N'account_name_test',

    N'account_name_test', N'account_number_test',

    N'account_code_test', N'account_operator_test',

    N'account_father_name_test', N'date_account_birth_test',

    N'ca_account_id_type_test', N'account_id_number_test',

    N'date_account_doc_issue_test',

    N'account_doc_issue_authority_test', N'', N'', N'', N'', N'',

    N'account_nationality_test', N'country_account_test',

    N'account_inc_status_test', N'ca_greek_tax_afm_test',

    N'ca_greek_tax_doy_test', N'ca_company_armae_test',

    N'ca_company_issue_authority_test',

    N'date_ca_company_issue_test', N'country_ca_company_test',

    N'No' )

    , ( N'682472', N'Sacha Blasenhauer', N'ggg', N'ggg',

    N'4444444444', N'44443423444', N'test', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'No' )

    , ( N'682472', N'Sacha Blasenhauer', N'2222', N'2222',

    N'2222222222', N'2222222222', N'22222', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'No' )

    , ( N'17388', N'Scottie Berwick', N'Scottie Berwick', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'Yes' )

    , ( N'', N'', N'', N'FNAME1', N'', N'', N'', N'FAN1',

    N'08/29/2013', N'DriversLicense', N'DI1', N'08/28/2013',

    N'IA1', N'S1', N'N1', N'PO1', N'CI1', N'Co1', N'NA1',

    N'Afghanistan', N'SA1', N'AFM1', N'DOY1', N'', N'', N'', N'',

    N'Yes' )

    , ( N'', N'', N'', N'FNAME2', N'', N'', N'', N'FATNAME2',

    N'08/01/2013', N'ID', N'ID2', N'08/02/2013', N'IA2', N'S2',

    N'N2', N'PO2', N'CI2', N'CO2', N'NA2', N'Afghanistan',

    N'SA2', N'', N'', N'', N'', N'', N'', N'Yes' )

    , ( N'', N'', N'', N'FNAME3', N'', N'', N'', N'FATNAME3',

    N'08/15/2013', N'Passport', N'PA3', N'08/16/2013', N'IA3',

    N'S3', N'N3', N'PO3', N'CI3', N'CO3', N'NA3',

    N'Åland Islands', N'SA3', N'', N'', N'AREM3', N'IAA3',

    N'08/17/2013', N'Albania', N'Yes' )

    , ( N'682472', N'Sacha Blasenhauer', N'abcd', N'abcd',

    N'111111111111', N'1111111111', N'11111111111', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'No' )

    , ( N'682472', N'Sacha Blasenhauer', N'', N'', N'', N'', N'',

    N'asd', N'09/01/2013', N'Passport', N'sdf', N'09/02/2013',

    N'sdf', N'sdf', N'sdf', N'dsf', N'sdf', N'sdf', N'sdf',

    N'Australia', N'', N'', N'', N'eee', N'eee', N'09/29/2013',

    N'Albania', N'No' )

    , ( N'17388', N'Scottie Berwick', N'Scottie Berwick', N'',

    N'7845124578', N'7845121212', N'sdfsd', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'Yes' )

    , ( N'', N'', N'Test', N'Test', N'1234567897', N'0000000000',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'Yes' )

    , ( N'', N'', N'SAT2', N'SAT2', N'4567891231', N'4545454545',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'Yes' )

    , ( N'17388', N'Scottie Berwick', N'Scottie Berwick', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'',

    N'', N'', N'', N'', N'', N'', N'', N'', N'', N'', N'Yes' );

    GO

    Then I created some sample data that would be easier to work with...

    --Data cleanup, let's get some sample data we can work with:

    WITH unique_IDs AS

    (

    SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY (SELECT ($))) AS rn, *

    FROM [dbo].[Test_AA]

    WHERE ID>0

    )

    SELECT ID, Name, CounterpartID, CounterpartName, SATName, NameOfBO, SATAccountNumber, SATCode, CurrentOperator, FatherName,

    DateOfBirth, TypeOfIdentification, IdenficationNumber, IssueDate, IssueAuthority, AddressStreet,AddressStreetNumber,

    AddressPostalCode, AddressCity, AddressCountry, Nationality, CountryOfTaxDomiciled, IncorporationStatus, GreekTaxIDNumber,

    GreekTaxOffice, GreekCompanyRegNumber, CompanyIssueAuthority, CompanyIssueDate, Country,[Joint Account]

    INTO #Test_AA_stg

    FROM unique_IDs

    WHERE rn=1;

    TRUNCATE TABLE Test_AA;

    INSERT Test_AA SELECT * FROM #Test_AA_stg;

    Your requirement is a little ambiguous, I suspect that you need a record for a single ID? If that be the case you could create an Inline Table Valued Function like so...

    CREATE FUNCTION dbo.data_block(@id int)

    RETURNS TABLE

    AS

    RETURN

    SELECT u.label+u.value AS xxx

    FROM Test_AA

    CROSS APPLY

    (VALUES

    ('ID: ', CONVERT(varchar(10), ID)),

    ('Name: ', Name),

    ('SATName: ', SATName),

    ('NameOfBO:', NameOfBO),

    ('SATAccountNumber:', SATAccountNumber),

    ('SATCode:', SATCode),

    ('CurrentOperator:', CurrentOperator),

    ('FatherName:', FatherName),

    ('DateOfBirth:', DateOfBirth),

    ('TypeOfIdentification:', TypeOfIdentification),

    ('IdenficationNumber:', IdenficationNumber),

    ('IssueDate:',IssueDate),

    ('IssueAuthority:',IssueAuthority),

    ('AddressStreet:',AddressStreet),

    ('AddressStreetNumber:',AddressStreetNumber),

    ('AddressPostalCode:',AddressPostalCode),

    ('AddressCity:',AddressCity),

    ('AddressCountry:',AddressCountry),

    ('Nationality:',Nationality),

    ('CountryOfTaxDomiciled:',CountryOfTaxDomiciled),

    ('IncorporationStatus:',IncorporationStatus),

    ('GreekTaxIDNumber:',GreekTaxIDNumber),

    ('GreekTaxOffice:',GreekTaxOffice),

    ('GreekCompanyRegNumber:',GreekCompanyRegNumber),

    ('CompanyIssueAuthority:',CompanyIssueAuthority),

    ('CompanyIssueDate:',CompanyIssueDate),

    ('Country:',Country),

    ('JointAccount:',[Joint Account])

    ) u(label,value)

    WHERE ID=@id;

    GO

    The technique I am using here is Dwain Camps' alternative to T-SQL UNPIVOT[/url].

    Let's us know if that helps.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • I've had a need to do similar block style within one column of the output, I used the FOR XML trick - something like:

    SELECT aa.ID

    , Stuff((Select char(10)+char(13)+

    'ID:' + isnull(iaa.ID,'N/A')

    + char(10)+char(13)+

    'Name:' + IsNull(Nullif(isnull(iaa.CounterpartLastName,'') + ', ' + isnull(iaa.CounterpartFirstName,''),', '),'Not Available') +

    char(10) + char(13) +

    'Phone:' + IsNull(Nullif(iaa.Phone,''),'Not Available')

    From [dbo].[Teast_AA] iaa

    Where iaa.ID = aa.ID

    FOR XML PATH(''), type

    ).value('(./text())[1]','varchar(2000)') ,1,2,'') BlockCol

    FROM [dbo].[Teast_AA] aa

    The char(10)+char(13) won't display in ssms grid results but should be fine in file or txt return, and does display correctly in ssrs and crystal reports.

  • Thanks for the suggestion. Actually I do not have any column on the basis of which I can segregate the blocks. I have to process one row as one block.

Viewing 5 posts - 1 through 4 (of 4 total)

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