Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Need help with dynamic sql query Expand / Collapse
Author
Message
Posted Wednesday, February 27, 2013 10:55 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:28 PM
Points: 13,103, Visits: 11,933
The code you posted has changed significantly from the previous version. Your original code had only 1 statement inside a cursor. The recent code you posted has lots more logic in it. The code I posted is only going to handle your first statement. I would bet this process is pretty slow if you have much data in these tables.

I would be willing to help you but you need to first help me. I need to have ddl and sample data along with the business rules of what you are trying to do here. Please take a few minutes to read the article found at 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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1424653
Posted Wednesday, February 27, 2013 10:59 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, June 29, 2013 11:09 AM
Points: 28, Visits: 31
Im new to sql. Where can I find the ddl?
Post #1424655
Posted Wednesday, February 27, 2013 11:13 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:28 PM
Points: 13,103, Visits: 11,933
jancas08 (2/27/2013)
Im new to sql. Where can I find the ddl?


Right click the table(s) or other objects in SSMS. The script as --> To new window.

What I am talking about here is the script to generate your table(s) in my system so I can work. Does that 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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1424660
Posted Wednesday, February 27, 2013 12:09 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, June 29, 2013 11:09 AM
Points: 28, Visits: 31
USE [janinetestdata]
GO

/****** Object: Table [dbo].[EDUCATION] Script Date: 02/27/2013 13:05:32 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[EDUCATION](
[ED_THREE] [float] NULL,
[ED_FOUR] [float] NULL,
[ED_FIVE] [float] NULL,
[ED_SIX] [float] NULL,
[PERSON_NUMBER] [float] NULL,
[zBarcode] [float] NULL
) ON [PRIMARY]

GO



[code="sql"]
USE [janinetestdata]
GO

/****** Object: Table [dbo].[FOURTEEN_ALL] Script Date: 02/27/2013 13:05:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[FOURTEEN_ALL](
[BatchNo] [float] NULL,
[zBarcode] [float] NULL,
[HHBarcode] [float] NULL,
[Person_No] [float] NULL,
[District] [float] NULL,
[URBAN_RURAL] [float] NULL,
[Cluster] [float] NULL,
[HHNUM] [float] NULL,
[Final_Result_Code] [float] NULL,
[Q1_5] [float] NULL,
[Q1_5_DKNS] [float] NULL,
[Q1_7] [float] NULL,
[Q1_8] [varchar](42) NULL,
[Q1_9] [float] NULL,
[Q1_9_2] [float] NULL,
[Q1_10a] [float] NULL,
[Q1_10b] [float] NULL,
[Q1_10c] [float] NULL,
[Q1_10_Total] [float] NULL,
[Q1_11] [float] NULL,
[Q1_12] [float] NULL,
[Q1_13] [float] NULL,
[Q1_14] [float] NULL,
[Q1_15] [float] NULL,
[Q1_15a] [float] NULL,
[Q1_16] [float] NULL,
[Q1_17] [varchar](70) NULL,
[Q1_18] [float] NULL,
[Q1_19] [float] NULL,
[Q1_20] [float] NULL,
[Q1_21] [float] NULL,
[Q1_22] [varchar](30) NULL,
[Q1_23] [varchar](30) NULL,
[Q1_24] [float] NULL,
[Q1_25] [float] NULL,
[Q1_26] [float] NULL,
[Q1_27] [float] NULL,
[Q1_28_MAIN] [float] NULL,
[Q1_28_OTHER] [float] NULL,
[Q1_28_PREVIOUS] [float] NULL,
[Q1_29_SEASONAL] [float] NULL,
[Q1_29_YEARROUND] [float] NULL,
[Q1_30_MAIN] [varchar](2) NULL,
[Q1_30_OTHER] [varchar](2) NULL,
[Q1_30_PREVIOUS] [varchar](2) NULL,
[Q1_31_MAIN] [varchar](2) NULL,
[Q1_31_OTHER] [varchar](2) NULL,
[Q1_31_PREVIOUS] [varchar](2) NULL,
[Q1_32_MAIN] [float] NULL,
[Q1_32_OTHER] [float] NULL,
[Q1_32_TOTAL] [float] NULL,
[Q1_33_MAIN] [float] NULL,
[Q1_33_OTHER] [float] NULL,
[Q1_33_TOTAL] [float] NULL,
[Q1_35] [float] NULL,
[Q1_37] [float] NULL,
[Q1_38] [float] NULL,
[Q1_39] [varchar](1) NULL,
[Q1_40] [varchar](1) NULL,
[Q1_41] [float] NULL,
[Q1_41_DKNS] [float] NULL,
[Q1_41a] [float] NULL,
[Q1_41b] [float] NULL,
[Q1_41b_DKNS] [float] NULL,
[Q1_42] [float] NULL,
[Q1_42_DKNS] [float] NULL,
[Q1_43] [float] NULL,
[Q1_44] [float] NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO






USE [janinetestdata]
GO

/****** Object: Table [dbo].[HH_ALL] Script Date: 02/27/2013 13:06:00 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[HH_ALL](
[BatchNo] [float] NULL,
[Cluster] [float] NULL,
[HHNUM] [float] NULL,
[zBarcode] [float] NULL,
[District] [float] NULL,
[URBAN_RURAL] [float] NULL,
[Final_Result_Code] [float] NULL,
[Person_No] [float] NULL
) ON [PRIMARY]

GO





USE [janinetestdata]
GO

/****** Object: Table [dbo].[HH_LISTING] Script Date: 02/27/2013 13:06:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[HH_LISTING](
[PERSON_NUMBER] [varchar](2) NULL,
[HL_THREE] [float] NULL,
[HL_FOUR] [float] NULL,
[HL_FIVE] [float] NULL,
[HL_SIX] [float] NULL,
[HL_SEVEN] [float] NULL,
[HL_SEVENB] [float] NULL,
[zBarcode] [float] NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO





USE [janinetestdata]
GO

/****** Object: Table [dbo].[TEST] Script Date: 02/27/2013 13:08:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[TEST](
[ERRORMESSAGE] [varchar](196) NULL,
[BATCH_NO] [float] NULL,
[zBarcode] [float] NULL,
[Incorrect_Value] [float] NULL,
[Columns_To_Fix] [varchar](17) NULL,
[Person_No] [float] NULL,
[Corrected_Value] [varchar](15) NULL,
[Err_Description] [varchar](302) NULL,
[Command] [varchar](29) NULL,
[ED_THREE] [float] NULL,
[ED_FOUR] [float] NULL,
[ED_FIVE] [float] NULL,
[ED_SIX] [float] NULL,
[Cluster] [float] NULL,
[HHNUM] [float] NULL,
[District] [float] NULL,
[URBAN_RURAL] [float] NULL,
[Final_Result_Code] [float] NULL,
[HL_THREE] [float] NULL,
[HL_FOUR] [float] NULL,
[HL_FIVE] [float] NULL,
[HL_SIX] [float] NULL,
[HL_SEVEN] [float] NULL,
[HL_SEVENB] [float] NULL,
[HHBarcode] [float] NULL,
[Q1_5] [float] NULL,
[Q1_5_KNS] [float] NULL,
[Q1_7] [float] NULL,
[Q1_8] [float] NULL,
[Q1_9] [float] NULL,
[Q1_9_2] [float] NULL,
[Q1_10a] [float] NULL,
[Q1_10b] [float] NULL,
[Q1_10c] [float] NULL,
[Q1_10_Total] [float] NULL,
[Q1_11] [float] NULL,
[Q1_12] [float] NULL,
[Q1_13] [float] NULL,
[Q1_14] [float] NULL,
[Q1_15] [float] NULL,
[Q1_15a] [float] NULL,
[Q1_16] [float] NULL,
[Q1_17] [float] NULL,
[Q1_18] [float] NULL,
[Q1_19] [float] NULL,
[Q1_20] [float] NULL,
[Q1_21] [float] NULL,
[Q1_22] [float] NULL,
[Q1_23] [float] NULL,
[Q1_24] [float] NULL,
[Q1_25] [float] NULL,
[Q1_26] [float] NULL,
[Q1_27] [float] NULL,
[Q1_28_MAIN] [float] NULL,
[Q1_28_OTHER] [float] NULL,
[Q1_28_PREVIOUS] [float] NULL,
[Q1_29_SEASONAL] [float] NULL,
[Q1_29_YEARROUND] [float] NULL,
[Q1_30_MAIN] [float] NULL,
[Q1_30_OTHER] [float] NULL,
[Q1_30_PREVIOUS] [float] NULL,
[Q1_31_MAIN] [float] NULL,
[Q1_31_OTHER] [float] NULL,
[Q1_31_PREVIOUS] [float] NULL,
[Q1_32_MAIN] [float] NULL,
[Q1_32_OTHER] [float] NULL,
[Q1_32_TOTAL] [float] NULL,
[Q1_33_MAIN] [float] NULL,
[Q1_33_OTHER] [float] NULL,
[Q1_33_TOTAL] [float] NULL,
[Q1_35] [float] NULL,
[Q1_37] [float] NULL,
[Q1_38] [float] NULL,
[Q1_39] [float] NULL,
[Q1_40] [float] NULL,
[Q1_41] [float] NULL,
[Q1_41_DKNS] [float] NULL,
[Q1_41a] [float] NULL,
[Q1_41b] [float] NULL,
[Q1_41b_DKNS] [float] NULL,
[Q1_42] [float] NULL,
[Q1_42_DKNS] [float] NULL,
[Q1_43] [float] NULL,
[Q1_44] [float] NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO





Post #1424677
Posted Wednesday, February 27, 2013 12:26 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, June 29, 2013 11:09 AM
Points: 28, Visits: 31



INSERT INTO [janinetestdata].[dbo].[EDUCATION]
([ED_THREE]
,[ED_FOUR]
,[ED_FIVE]
,[ED_SIX]
,[PERSON_NUMBER]
,[zBarcode])
VALUES
(<ED_THREE, float,>
,<ED_FOUR, float,>
,<ED_FIVE, float,>
,<ED_SIX, float,>
,<PERSON_NUMBER, float,>
,<zBarcode, float,>)
GO


INSERT INTO [janinetestdata].[dbo].[FOURTEEN_ALL]
([BatchNo]
,[zBarcode]
,[HHBarcode]
,[Person_No]
,[District]
,[URBAN_RURAL]
,[Cluster]
,[HHNUM]
,[Final_Result_Code]
,[Q1_5]
,[Q1_5_DKNS]
,[Q1_7]
,[Q1_8]
,[Q1_9]
,[Q1_9_2]
,[Q1_10a]
,[Q1_10b]
,[Q1_10c]
,[Q1_10_Total]
,[Q1_11]
,[Q1_12]
,[Q1_13]
,[Q1_14]
,[Q1_15]
,[Q1_15a]
,[Q1_16]
,[Q1_17]
,[Q1_18]
,[Q1_19]
,[Q1_20]
,[Q1_21]
,[Q1_22]
,[Q1_23]
,[Q1_24]
,[Q1_25]
,[Q1_26]
,[Q1_27]
,[Q1_28_MAIN]
,[Q1_28_OTHER]
,[Q1_28_PREVIOUS]
,[Q1_29_SEASONAL]
,[Q1_29_YEARROUND]
,[Q1_30_MAIN]
,[Q1_30_OTHER]
,[Q1_30_PREVIOUS]
,[Q1_31_MAIN]
,[Q1_31_OTHER]
,[Q1_31_PREVIOUS]
,[Q1_32_MAIN]
,[Q1_32_OTHER]
,[Q1_32_TOTAL]
,[Q1_33_MAIN]
,[Q1_33_OTHER]
,[Q1_33_TOTAL]
,[Q1_35]
,[Q1_37]
,[Q1_38]
,[Q1_39]
,[Q1_40]
,[Q1_41]
,[Q1_41_DKNS]
,[Q1_41a]
,[Q1_41b]
,[Q1_41b_DKNS]
,[Q1_42]
,[Q1_42_DKNS]
,[Q1_43]
,[Q1_44])
VALUES
(<BatchNo, float,>
,<zBarcode, float,>
,<HHBarcode, float,>
,<Person_No, float,>
,<District, float,>
,<URBAN_RURAL, float,>
,<Cluster, float,>
,<HHNUM, float,>
,<Final_Result_Code, float,>
,<Q1_5, float,>
,<Q1_5_DKNS, float,>
,<Q1_7, float,>
,<Q1_8, varchar(42),>
,<Q1_9, float,>
,<Q1_9_2, float,>
,<Q1_10a, float,>
,<Q1_10b, float,>
,<Q1_10c, float,>
,<Q1_10_Total, float,>
,<Q1_11, float,>
,<Q1_12, float,>
,<Q1_13, float,>
,<Q1_14, float,>
,<Q1_15, float,>
,<Q1_15a, float,>
,<Q1_16, float,>
,<Q1_17, varchar(70),>
,<Q1_18, float,>
,<Q1_19, float,>
,<Q1_20, float,>
,<Q1_21, float,>
,<Q1_22, varchar(30),>
,<Q1_23, varchar(30),>
,<Q1_24, float,>
,<Q1_25, float,>
,<Q1_26, float,>
,<Q1_27, float,>
,<Q1_28_MAIN, float,>
,<Q1_28_OTHER, float,>
,<Q1_28_PREVIOUS, float,>
,<Q1_29_SEASONAL, float,>
,<Q1_29_YEARROUND, float,>
,<Q1_30_MAIN, varchar(2),>
,<Q1_30_OTHER, varchar(2),>
,<Q1_30_PREVIOUS, varchar(2),>
,<Q1_31_MAIN, varchar(2),>
,<Q1_31_OTHER, varchar(2),>
,<Q1_31_PREVIOUS, varchar(2),>
,<Q1_32_MAIN, float,>
,<Q1_32_OTHER, float,>
,<Q1_32_TOTAL, float,>
,<Q1_33_MAIN, float,>
,<Q1_33_OTHER, float,>
,<Q1_33_TOTAL, float,>
,<Q1_35, float,>
,<Q1_37, float,>
,<Q1_38, float,>
,<Q1_39, varchar(1),>
,<Q1_40, varchar(1),>
,<Q1_41, float,>
,<Q1_41_DKNS, float,>
,<Q1_41a, float,>
,<Q1_41b, float,>
,<Q1_41b_DKNS, float,>
,<Q1_42, float,>
,<Q1_42_DKNS, float,>
,<Q1_43, float,>
,<Q1_44, float,>)
GO


INSERT INTO [janinetestdata].[dbo].[HH_ALL]
([BatchNo]
,[Cluster]
,[HHNUM]
,[zBarcode]
,[District]
,[URBAN_RURAL]
,[Final_Result_Code]
,[Person_No])
VALUES
(<BatchNo, float,>
,<Cluster, float,>
,<HHNUM, float,>
,<zBarcode, float,>
,<District, float,>
,<URBAN_RURAL, float,>
,<Final_Result_Code, float,>
,<Person_No, float,>)
GO


INSERT INTO [janinetestdata].[dbo].[HH_LISTING]
([PERSON_NUMBER]
,[HL_THREE]
,[HL_FOUR]
,[HL_FIVE]
,[HL_SIX]
,[HL_SEVEN]
,[HL_SEVENB]
,[zBarcode])
VALUES
(<PERSON_NUMBER, varchar(2),>
,<HL_THREE, float,>
,<HL_FOUR, float,>
,<HL_FIVE, float,>
,<HL_SIX, float,>
,<HL_SEVEN, float,>
,<HL_SEVENB, float,>
,<zBarcode, float,>)
GO










Post #1424684
Posted Wednesday, February 27, 2013 12:28 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, June 29, 2013 11:09 AM
Points: 28, Visits: 31


INSERT INTO [janinetestdata].[dbo].[EDUCATION] VALUES(2,3,4,6,2,4398)

INSERT INTO [janinetestdata].[dbo].[EDUCATION] VALUES(2,3,7,5,3,4237)



INSERT INTO [janinetestdata].[dbo].[HH_ALL] VALUES(36144 , 2 , 3 , 21227 , 5 , 6 , 8 , 1 )

INSERT INTO [janinetestdata].[dbo].[HH_ALL] VALUES(36157 , 2 , 3 , 5678 , 5 , 6 , 8 , 1 )


INSERT INTO [janinetestdata].[dbo].[HH_LISTING] VALUES('1' , 3 , 4 , 6 , 8 , 6 , 8 , 15011 )

INSERT INTO [janinetestdata].[dbo].[HH_LISTING] VALUES('1' , 3 , 4 , 6 , 8 , 6 , 8 , 15011 )


INSERT INTO [janinetestdata].[dbo].[FOURTEEN_ALL] VALUES(36122 , 21593 , 3 , '1' , 4 , 4 , 7 , 9 , 3 , 4 , 5 , 5 , '5' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , '5' , 5 , 5 , 5 , 5 , '5' , '5' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , '5' , '5' , '5' , '5' , '5' , '5' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , '5' , '5' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5)


INSERT INTO [janinetestdata].[dbo].[FOURTEEN_ALL] VALUES(36124 , 22019 , 2 , '1' , 2 , 4 , 7 , 9 , 4 , 6 , 4 , 6 , '5' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , '5' , 5 , 5 , 5 , 5 , '5' , '5' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , '5' , '5' , '5' , '5' , '5' , '5' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , '5' , '5' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5)


Post #1424686
Posted Wednesday, February 27, 2013 12:31 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, June 29, 2013 11:09 AM
Points: 28, Visits: 31
Now, my rule is that: basically where ever there is an update you update the the table with the values. You determine what table to update by the error message.

If the error message is ED* then you update the education table
If the error message is HH* then you update the HH_ALL table
If the error message is HL* then you update the HL table
If the error message is O14* then you update the FOURTEEN_ALL table


The same rules apply when you delete and insert into a table.
Post #1424690
Posted Wednesday, February 27, 2013 12:38 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:28 PM
Points: 13,103, Visits: 11,933
jancas08 (2/27/2013)
Now, my rule is that: basically where ever there is an update you update the the table with the values. You determine what table to update by the error message.

If the error message is ED* then you update the education table
If the error message is HH* then you update the HH_ALL table
If the error message is HL* then you update the HL table
If the error message is O14* then you update the FOURTEEN_ALL table


The same rules apply when you delete and insert into a table.


I assume you mean ERRORMESSAGE from ERROR_FILE_OW4_4? There is no data in this table. Keep in mind I don't know your system, I am not familiar with your project and I can't see over your shoulder.

As a side note, your tables suffer greatly from a lack of normalization and there are no indexes, keys or referential integrity.


_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1424694
Posted Wednesday, February 27, 2013 12:42 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, June 29, 2013 11:09 AM
Points: 28, Visits: 31
The errormessage is indicated in TEST; its the lasts ddl I placed.
Post #1424699
Posted Wednesday, February 27, 2013 12:49 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, June 29, 2013 11:09 AM
Points: 28, Visits: 31
Okay I will simplify my steps:


The TEST ddl is the data that has commands that specify changes to the tables.

My objective is to do those specified commands which can either be insert update or delete.

The TEST ddl has a column named COMMAND which will have insert update or delete.
The TEST ddl has a column named ERRORMESSAGE; this specifies which table to run the commands on.
So, my rules are:

O14* dictates use of FOURTEEN_ALL table
HH* dictates use of HH_ALL table
HL* dictates use of HH_LISTING table
ED* dicatates use of EDUCATION table

All that I am to do is run the commands and display what commands are being executed.
Post #1424707
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse