Solved

MS SQL Server 2008 - Problem bounding a Table

Posted on 2013-01-03
10
401 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
ID: 38741728
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
ID: 38741849
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
ID: 38741877
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
ID: 38741921
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
ID: 38741979
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
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 

Author Comment

by:Amour22015
ID: 38743609
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
ID: 38743851
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
ID: 38744091
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
ID: 38744221
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
ID: 38773835
Please close this post, I answered my own question....
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.

920 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

16 Experts available now in Live!

Get 1:1 Help Now