Solved

MS SQL Server 2008 - Problem bounding a Table

Posted on 2013-01-03
10
399 Views
Last Modified: 2013-01-14
Hi and thanks,

If I just use this (it it fine and works):
USE [MVP_Demo]
GO
/****** Object:  StoredProcedure [dbo].[PWC_VALIDMAINSP]    Script Date: 01/03/2013 14:16:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		
-- Create date: 11/15/2012
-- Description:	Creates a temporary table for Validations
-- =============================================
ALTER PROCEDURE [dbo].[PWC_VALIDMAINSP] 
AS
BEGIN
IF  EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[TEMP_PWC_VALTEMP]') AND type in (N'U'))
DROP TABLE [dbo].[TEMP_PWC_VALMAIN]

CREATE TABLE [dbo].[TEMP_PWC_VALMAIN]
(
 mpropertynumber varchar(255),
 Valnum varchar(255),
 )

DECLARE @sql nvarchar(4000)
DECLARE @Valnum nvarchar(4000)
DECLARE @Valid1 nvarchar(4000)
DECLARE @Valid2 nvarchar(4000)

DECLARE main_cursor CURSOR FOR
SELECT Valid1, Valid2, Valnum FROM dbo.TEMP_PWC_ValTable
Where CAST(Active as varchar(10)) = '1' And Filename = 'MAIN'
--Where CAST(Valnum as varchar(10)) in ('1', '25')

OPEN main_cursor
SET @sql = ''
SET @Valnum = ''
SET @Valid1 = ''
SET @Valid2 = ''
-- Perform the first fetch.
FETCH NEXT FROM main_cursor
INTO @Valid1, @Valid2, @Valnum


-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
      IF @sql = ''
      BEGIN

            IF @Valid2 = ''
                  SET @sql = 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] where ' + @Valid1
            ELSE
                  SET @sql = 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] where ' + @Valid1 + ' and ' + @Valid2
      END
      ELSE
      BEGIN
            IF @Valid2 = ''
                          SET @sql = @sql +' union ' + 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] where ' + @Valid1
            ELSE
                    SET @sql = @sql +' union ' + 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] where ' + @Valid1 + ' and ' + @Valid2
      END
   

   -- This is executed as long as the previous fetch succeeds.
   FETCH NEXT FROM main_cursor
      INTO @Valid1, @Valid2, @Valnum
      
      
END


CLOSE main_cursor
DEALLOCATE main_cursor

   INSERT [TEMP_PWC_VALMAIN] EXEC sp_executesql @sql
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

END

Open in new window


But when I try to add another Table (using this code):
USE [MVP_Demo]
GO
/****** Object:  StoredProcedure [dbo].[TEMP_PWC_VALIDATIONSP]    Script Date: 01/03/2013 14:17:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		
-- Create date: 11/15/2012
-- Description:	Creates a temporary table for Validations
-- =============================================
ALTER PROCEDURE [dbo].[TEMP_PWC_VALIDATIONSP] 
AS
BEGIN
IF  EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[TEMP_PWC_VALTEMP]') AND type in (N'U'))
DROP TABLE [dbo].[TEMP_PWC_VALTEMP]

CREATE TABLE [dbo].[TEMP_PWC_VALTEMP]
(
 mpropertynumber varchar(255),
 Valnum varchar(255),
 )

DECLARE @sql nvarchar(4000)
DECLARE @sql1 nvarchar(4000)
DECLARE @Valnum nvarchar(4000)
DECLARE @Valid1 nvarchar(4000)
DECLARE @Valid2 nvarchar(4000)

DECLARE main_cursor CURSOR FOR
SELECT Valid1, Valid2, Valnum FROM dbo.TEMP_PWC_ValTable
Where CAST(Active as varchar(10)) = '1'
--Where CAST(Valnum as varchar(10)) in ('1', '25')

OPEN main_cursor
SET @Sql1 = ''
SET @sql = ''
SET @Valnum = ''
SET @Valid1 = ''
SET @Valid2 = ''
-- Perform the first fetch.
FETCH NEXT FROM main_cursor
INTO @Valid1, @Valid2, @Valnum


-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
      IF @sql = ''
      BEGIN

            IF @Valid2 = ''
                  SET @sql = 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] where ' + @Valid1
            ELSE
                  SET @sql = 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] where ' + @Valid1 + ' and ' + @Valid2
      END
      ELSE
      BEGIN
            IF @Valid2 = ''
                          SET @sql = @sql +' union ' + 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] where ' + @Valid1
            ELSE
                    SET @sql = @sql +' union ' + 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] where ' + @Valid1 + ' and ' + @Valid2
      END
   

   -- This is executed as long as the previous fetch succeeds.
   FETCH NEXT FROM main_cursor
      INTO @Valid1, @Valid2, @Valnum
      
      
END

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
      IF @sql1 = ''
      BEGIN

            IF @Valid2 = ''
                  SET @sql1 = 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_CHARACTERISTICS] where ' + @Valid1
            ELSE
                  SET @sql1 = 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_CHARACTERISTICS] where ' + @Valid1 + ' and ' + @Valid2
      END
      ELSE
      BEGIN
            IF @Valid2 = ''
                          SET @sql1 = @sql1 +' union ' + 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_CHARACTERISTICS] where ' + @Valid1
            ELSE
                    SET @sql1 = @sql1 +' union ' + 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_CHARACTERISTICS] where ' + @Valid1 + ' and ' + @Valid2
      END
   

   -- This is executed as long as the previous fetch succeeds.
   FETCH NEXT FROM main_cursor
      INTO @Valid1, @Valid2, @Valnum
      
      
END
CLOSE main_cursor
DEALLOCATE main_cursor

   INSERT [TEMP_PWC_VALTEMP] EXEC sp_executesql @sql, @Sql1
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

END

Open in new window


I get errors:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "TEMP_PWC_Report_SP_CHARACTERISTICS.YearBuilt" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "TEMP_PWC_Report_SP_CHARACTERISTICS.FinishedLivingArea" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "TEMP_PWC_Report_SP_CHARACTERISTICS.YearBuilt" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "TEMP_PWC_Report_SP_CHARACTERISTICS.FinishedLivingArea" could not be bound.

Open in new window


What I am doing is creating a Validation report to check if any errors exist in the Table(s)

Table: ValTable has functions that checks the Table(s)

Thanks for any help
0
Comment
Question by:Amour22015
  • 6
  • 4
10 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Is the TEMP_PWC_Report_SP_CHARACTERISTICS structure the same as the other tables in the check? In other words, is it possible that the table containing the expected columns thinks there is a YearBuilt column when there really is not one. It would seem that the issue is the dynamic SQL is creating a SELECT on a column that does not truly exist in the table if I am not missing something.
0
 

Author Comment

by:Amour22015
Comment Utility
I think you are correct,

But how to get around the problem?

I am knew to all this.

Seems like it is not resetting the fields before it reads the next Table?

Thanks for any help
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
In the top procedure, the WHERE clause for the cursor is:
Where CAST(Active as varchar(10)) = '1' And Filename = 'MAIN'

I do not see the latter portion of the filter in the second procedure. In addition, you have two WHILE @@FETCH_STATUS = 0 in the second procedure; therefore, you may need to close the first cursor and open a new one if the second block requires a different Filename OR you will need to add a condition that checks the Filename applies on each iteration through the WHILE loop.
0
 

Author Comment

by:Amour22015
Comment Utility
Hi,

Like I mentioned that when only one Table (or I should have mentioned under one condition in the same Table) is introduced it works fine:
Where CAST(Active as varchar(10)) = '1' And Filename = 'MAIN'

There are 2 conditions in one Table MAIN And RESID

So If I onle have:
Where CAST(Active as varchar(10)) = '1'

I will get both conditions.
And that condition is True (1)
Active = True


Thanks
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
But isn't the table in question the one that determines the columns? It would be good to find whatever table or process determines what columns to verify. Without this knowledge, it is hard to troubleshoot as there is a lot of dynamic SQL happening here apparently.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:Amour22015
Comment Utility
What happens:

Stored Procedure reads Table ValTable from fields Valid1 and Valid2 Both of the fields has functions statements in them.

Example:
Valid1:
[TEMP_PWC_Report_SP_MAIN].[mclassificationid] = '011'

Valid2:
[TEMP_PWC_Report_SP_CHARACTERISTICS].[ExteriorWallCodeCode] Not Between '00' And '72'

What this does is checks the Main table for id to = 011
And
Checks Characteristics for exteriorwallcodecode not between 00 and 72

If both of the conditions are true then a record is added to the Table (ValTemp) using fields: propertyNumbers, and Valnum.

thanks
0
 

Author Comment

by:Amour22015
Comment Utility
Ok I tried changing to:
SET @sql = 'SELECT mpropertynumber, ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] Join [TEMP_PWC_Report_SP_CHARACTERISTICS] ON [TEMP_PWC_Report_SP_CHARACTERISTICS].[mpropertynumber] = [TEMP_PWC_Report_SP_MAIN].[mpropertynumber] where' + @Valid1

Noticed that I added the Join Statement.

But now I am getting error:
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string 'TEMP_PWC_Report_SP_'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'TEMP_PWC_Report_SP_'.


Please help and thanks
0
 

Accepted Solution

by:
Amour22015 earned 0 total points
Comment Utility
I was able to solve the problem.

I did not have a "Join" "ON"
Statements in the
@Sql =

Needed this to Join the Table(s)

Thanks for your help:

USE [MVP_Demo]
GO
/****** Object:  StoredProcedure [dbo].[TEMP_PWC_VALIDATIONSP]    Script Date: 01/04/2013 10:43:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:            
-- Create date: 11/15/2012
-- Description:      Creates a temporary table for Validations
-- =============================================
ALTER PROCEDURE [dbo].[TEMP_PWC_VALIDATIONSP]
AS
BEGIN
IF  EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[TEMP_PWC_VALTEMP]') AND type in (N'U'))
DROP TABLE [dbo].[TEMP_PWC_VALTEMP]

CREATE TABLE [dbo].[TEMP_PWC_VALTEMP]
(
 mpropertynumber varchar(255),
 Valnum varchar(255),
 )

DECLARE @sql nvarchar(max)
DECLARE @Valnum nvarchar(4000)
DECLARE @Valid1 nvarchar(4000)
DECLARE @Valid2 nvarchar(4000)

DECLARE main_cursor CURSOR FOR
SELECT Valid1, Valid2, Valnum FROM dbo.TEMP_PWC_ValTable
Where CAST(Active as varchar(10)) = '1'
--Where CAST(Valnum as varchar(10)) in ('1', '25')

OPEN main_cursor
SET @sql = ''
SET @Valnum = ''
SET @Valid1 = ''
SET @Valid2 = ''
-- Perform the first fetch.
FETCH NEXT FROM main_cursor
INTO @Valid1, @Valid2, @Valnum


-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
      IF @sql = ''
      BEGIN

            IF @Valid2 = ''
                  SET @sql = 'SELECT [TEMP_PWC_Report_SP_MAIN].[mpropertynumber], ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] Join [TEMP_PWC_Report_SP_CHARACTERISTICS] ON [TEMP_PWC_Report_SP_CHARACTERISTICS].[mpropertynumber] = [TEMP_PWC_Report_SP_MAIN].[mpropertynumber] where ' + @Valid1
            ELSE
                  SET @sql = 'SELECT [TEMP_PWC_Report_SP_MAIN].[mpropertynumber], ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] Join [TEMP_PWC_Report_SP_CHARACTERISTICS] ON [TEMP_PWC_Report_SP_CHARACTERISTICS].[mpropertynumber] = [TEMP_PWC_Report_SP_MAIN].[mpropertynumber] where ' + @Valid1 + ' and ' + @Valid2
      END
      ELSE
      BEGIN
            IF @Valid2 = ''
                          SET @sql = @sql +' union ' + 'SELECT [TEMP_PWC_Report_SP_MAIN].[mpropertynumber], ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] Join [TEMP_PWC_Report_SP_CHARACTERISTICS] ON [TEMP_PWC_Report_SP_CHARACTERISTICS].[mpropertynumber] = [TEMP_PWC_Report_SP_MAIN].[mpropertynumber] where ' + @Valid1
            ELSE
                    SET @sql = @sql +' union ' + 'SELECT [TEMP_PWC_Report_SP_MAIN].[mpropertynumber], ' + @Valnum + ' from [TEMP_PWC_Report_SP_MAIN] Join [TEMP_PWC_Report_SP_CHARACTERISTICS] ON [TEMP_PWC_Report_SP_CHARACTERISTICS].[mpropertynumber] = [TEMP_PWC_Report_SP_MAIN].[mpropertynumber] where ' + @Valid1 + ' and ' + @Valid2
      END
   

   -- This is executed as long as the previous fetch succeeds.
   FETCH NEXT FROM main_cursor
      INTO @Valid1, @Valid2, @Valnum
     
     
END

CLOSE main_cursor
DEALLOCATE main_cursor

   INSERT [TEMP_PWC_VALTEMP] EXEC sp_executesql @sql
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

END
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Makes sense. So you were calling columns that were in another table. Glad you found it.

Best regards and happy coding,

Kevin
0
 

Author Closing Comment

by:Amour22015
Comment Utility
Please close this post, I answered my own question....
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now