Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9028665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:02:49+00:00 2026-06-16T07:02:49+00:00

I have a table in SQL Server 2008 which contains custom validation criteria in

  • 0

I have a table in SQL Server 2008 which contains custom validation criteria in the form of expressions stored as text, e.g.

StagingTableID    CustomValidation
----------------------------------
1                 LEN([mobile])<=30
3                 [Internal/External] IN ('Internal','External')
3                 ([Internal/External] <> 'Internal') OR (LEN([Contact Name])<=100)
...

I am interested in determining whether all rows in a table pass the conditional statement. For this purpose I am writing a validation stored procedure which checks whether all values in a given field in a given table meet the given condition(s). SQL is not my forte, so after reading this questions this is my first stab at the problem:

EXEC sp_executesql N'SELECT @passed = 0 WHERE EXISTS (' +
                     N'SELECT * FROM (' +
                       N'SELECT CASE WHEN ' + @CustomValidationExpr + N' THEN 1 ' +
                       N'ELSE 0 END AS ConditionalTest ' +
                       N'FROM ' + @StagingTableName +
                     N')t ' +
                     N'WHERE t.ConditionalTest = 0)'
                  ,N'@passed BIT OUTPUT'
                  ,@passed = @PassedCustomValidation OUTPUT

However, I’m not sure if the nested queries can be re-written as one, or if there is an entirely better way for testing for validity of all rows in this scenario?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:02:50+00:00Added an answer on June 16, 2026 at 7:02 am

    You should be able to reduce by at least one subquery like this:

    EXEC sp_executesql N'SELECT @passed = 0 WHERE EXISTS (' +
                           N'SELECT 1 FROM ' +  @StagingTableName +
                           N'WHERE NOT(' + @CustomValidationExpr + N')) ' +
                      ,N'@passed BIT OUTPUT'
                      ,@passed = @PassedcustomValidation OUTPUT
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table in Sql Server 2008 R2 which contains data stored
I have a table which contains my ads that can be searched in sql-server-2008.
I have an audit table in my SQL Server 2008 database which contains the
I have a table in my SQL Server 2008 database called dbo.app_additional_info which contains
I have a SQL Server 2008 table which contains an external user reference currently
I have a table in Sql Server 2008 Express which contains 18 million records.
I use SQL Server 2008, C#, I have a table which contains about 20000
I have mapped Entity framework entities. Each table in SQL Server 2008 contains Timestamp
So I have a Customers table on a sql 2008 server which I bind
I'm using SQL Server 2008 R2 and I have a database which contains two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.