Share via

What is wrong with this sql statement?

MR BILL 266 Reputation points
2021-07-19T12:28:51.837+00:00

I'm expecting this statement to find about 45 Users that do not have this setting in the settings table but it isn't inserting any records. The sql statement is:

INSERT INTO [dbo].Settings SELECT 'ALLOW_WEB_APPROVALS', '1', [Login] FROM Users where [Login] not in (Select UserName from Settings where PropertyField='ALLOW_WEB_APPROVALS')

The username field in settings is the Login field in Users table. What am I doing wrong?

Any help is appreciated.

Thanks

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
0 comments No comments

Answer accepted by question author
  1. Olaf Helper 47,616 Reputation points
    2021-07-19T12:41:40.917+00:00

    Please post table design as DDL, some sample data as DML statement and the expected result.

    Is there may be a UserName NULL? A compare on NULL ends in undefined result and so may don't return any result. Filter NULL out =>

    INSERT INTO [dbo].[Settings](PropertyField, ValueField, UserName) 
    SELECT 'ALLOW_WEB_APPROVALS', '1', [Login] 
    FROM Users 
    where [Login] not in (Select UserName 
                          from Settings 
                          where PropertyField='ALLOW_WEB_APPROVALS'
                                and not UserName IS NULL)
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.