What is wrong with this sql statement?

MR BILL 256 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

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,309 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,522 questions
0 comments No comments
{count} votes

Accepted answer
  1. Olaf Helper 39,181 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 Answers by the question author, which helps users to know the answer solved the author's problem.