Click here to Skip to main content
15,920,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I found this on the web under "TSQL - Solve it YOUR Way - Finding the percentage of NULL values for each column in a table" but when I run it I get the following error:

Msg 102, Level 15, State 1, Line 20
Incorrect syntax near ')'.

I can't find out why.


SQL
SET NOCOUNT ON
DECLARE @Statement NVARCHAR(MAX) = ''
DECLARE @Statement2 NVARCHAR(MAX) = ''
DECLARE @FinalStatement NVARCHAR(MAX) = ''
DECLARE @TABLE_SCHEMA SYSNAME = 'Production'
DECLARE @TABLE_NAME SYSNAME = 'Product'
SELECT
        @Statement = @Statement + 'SUM(CASE WHEN ' + COLUMN_NAME + ' IS NULL THEN 1 ELSE 0 END) AS ' + COLUMN_NAME + ',' + CHAR(13) ,
        @Statement2 = @Statement2 + COLUMN_NAME + '*100 / OverallCount AS ' + COLUMN_NAME + ',' + CHAR(13) FROM
INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TABLE_NAME AND TABLE_SCHEMA = @TABLE_SCHEMA
IF @@ROWCOUNT = 0
        RAISERROR('TABLE OR VIEW with schema "%s" and name "%s" does not exists or you do not have appropriate permissions.',16,1, @TABLE_SCHEMA, @TABLE_NAME)
ELSE
BEGIN
        SELECT @FinalStatement =
                'SELECT ' + LEFT(@Statement2, LEN(@Statement2) -2) + ' FROM (SELECT ' + LEFT(@Statement, LEN(@Statement) -2) +
                ', COUNT(*) AS OverallCount FROM ' + @TABLE_SCHEMA + '.' + @TABLE_NAME + ') SubQuery'
        EXEC(@FinalStatement)

Posted
Comments
PIEBALDconsult 9-Oct-15 11:47am    
I think you're missing a closing apostrophe after the WHERE clause. And you don't show an END either.
dhMac 9-Oct-15 12:12pm    
Where would the apostrophe go? ...and where would the END be put? To be honest I can't really find where the WHERE clause ends. I'm sorry for not knowing...this is a project due, it's exactly what I need but I can't seem to get it just right. I'm sure it works except for the error message. Smile | Smile | :)
John C Rayan 13-Oct-15 4:18am    
Hi dhMac

You are missing an END for BEGIN. You could see my solution at the bottom. If you add END at the end you will be fine.

Please mark the solution as accepted if you are happy with the solution so as to close the thread.
Richard Deeming 9-Oct-15 13:23pm    
Your query works fine for me on one of my tables. Do you have any strange column names? Try wrapping each COLUMN_NAME with a QUOTENAME(...):

@Statement = @Statement + 'SUM(CASE WHEN ' + QUOTENAME(COLUMN_NAME) + ' IS NULL THEN 1 ELSE 0 END) AS ' + QUOTENAME(COLUMN_NAME) + ',' + CHAR(13) ,
@Statement2 = @Statement2 + QUOTENAME(COLUMN_NAME) + ' * 100 / OverallCount AS ' + QUOTENAME(COLUMN_NAME) + ',' + CHAR(13)

1 solution

Hi dhMac

You are missing the END for BEGIN. See the last line.

SQL
SET NOCOUNT ON

DECLARE @Statement NVARCHAR(MAX) = ''

DECLARE @Statement2 NVARCHAR(MAX) = ''

DECLARE @FinalStatement NVARCHAR(MAX) = ''

DECLARE @TABLE_SCHEMA SYSNAME = 'Production'

DECLARE @TABLE_NAME SYSNAME = 'Product'

SELECT

        @Statement = @Statement + 'SUM(CASE WHEN ' + COLUMN_NAME + ' IS NULL THEN 1 ELSE 0 END) AS ' + COLUMN_NAME + ',' + CHAR(13) ,

        @Statement2 = @Statement2 + COLUMN_NAME + '*100 / OverallCount AS ' + COLUMN_NAME + ',' + CHAR(13) FROM

INFORMATION_SCHEMA.COLUMNS

WHERE TABLE_NAME = @TABLE_NAME AND TABLE_SCHEMA = @TABLE_SCHEMA

IF @@ROWCOUNT = 0

        RAISERROR('TABLE OR VIEW with schema "%s" and name "%s" does not exists or you do not have appropriate permissions.',16,1, @TABLE_SCHEMA, @TABLE_NAME)

ELSE

BEGIN

   SELECT @FinalStatement =

                'SELECT ' + LEFT(@Statement2, LEN(@Statement2) -2) + ' FROM (SELECT ' + LEFT(@Statement, LEN(@Statement) -2) +

                ', COUNT(*) AS OverallCount FROM ' + @TABLE_SCHEMA + '.' + @TABLE_NAME + ') SubQuery'

        EXEC(@FinalStatement)

END
 
Share this answer
 
v2
Comments
Richard Deeming 12-Oct-15 14:12pm    
Your "answer" just seems to be a repost of the code from the question.

What did you change?
John C Rayan 12-Oct-15 16:13pm    
There is an END missing for BEGIN.
Richard Deeming 12-Oct-15 16:14pm    
So how about adding that explanation to your answer, rather than expecting the user to play spot-the-difference with your code block?
John C Rayan 12-Oct-15 16:34pm    
OP is expected to take the solution and he will definitely find out the extra line. It is not that difficult for a developer I guess
John C Rayan 13-Oct-15 1:57am    
Can you tell me the reason for down voting the solution. It works..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900