1

I want to be able to check if we have a column and if not, then we just want to add an empty column,

    IF Users.[parties] = '' OR NULL
        BEGIN
            SELECT [parties] 
            FROM Users 
            UNION 
            SELECT 'Empty'
        END

The Users.[parties], we check to see if we have a column but if we don't, it will result in a crash, in the case for this event I thought it would be best just to add an empty column with the name of Empty but I can't get the code to work above.

If we do have columns, the results will be something like...

ColumnsName    ColumnAge
data             33
data             22

But when there isn't a column, it crashes and ideally I would like it to just have an empty column like this,

EmptyColumn
4
  • 4
    I don't follow the problem and what you are trying to do, but union adds row(s), not columns. Commented Feb 6, 2019 at 13:31
  • Users.Parties can't even be referenced in that IF, it's not within a subquery so the server won't know the context of a what it is. Commented Feb 6, 2019 at 13:35
  • So your query should return a 2col x Nrow result if there are n rows and 1col x 1row if there are none? Commented Feb 6, 2019 at 13:39
  • Possible duplicate of How to check if a column exists in a SQL Server table? Commented Feb 6, 2019 at 13:43

3 Answers 3

1

The code below checks whether a column exists in the table, in our case the name of the column is columnName and the name of the table is tableName.

IF COL_LENGTH('schemaName.tableName', 'columnName') IS NOT NULL
    BEGIN
        -- Column exists
       SELECT [parties] FROM Users
    END
ELSE 
    BEGIN
        -- Column does not exists
        SELECT 'Empty'[parties]
    END
Sign up to request clarification or add additional context in comments.

Comments

0

I think you just want

IF EXISTS(
           SELECT 1 
           FROM Sys.Columns 
           WHERE Name = N'parties'
                 AND 
                 Object_ID = Object_ID(N'SchemaName.Users')
         )
BEGIN
  SELECT parties
  FROM Users;
END
ELSE
BEGIN
  SELECT 'EmptyColumn' EmptyColumn -- or NULL EmptyColumn 
  FROM Users; 
END

Comments

0

I'll try with this: (I'm not sure it works)

select case when ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) =0 --count rows
          then 'empty'  -- if 0 output empty
          else parties end as parties --else ouputs the result
from your_table

This is a more 'standard' approach

CREATE VIEW user_filled as
SELECT [parties] 
FROM Users 
UNION 
SELECT 'EMPTY'

and when you query it (if needed -> on count(*))

select count(*) 
from user_filled
where parties <> 'EMPTY'

on join

select *
from user_filled join other_table 
      on (user_filled <> 'EMPTY and userfilled.key= other_table.key)

NOTE: put the clause into the ON so it's filtered out BEFORE the join is made

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.