---
title: "How to retrieve all row from all tables with where condition"  
description: "How to retrieve all row from all tables with where condition"  
author: "Royce Roy"  
published: 2015-05-25  
updated: 2015-05-25  
canonical: https://www.mindstick.com/forum/23259/how-to-retrieve-all-row-from-all-tables-with-where-condition  
category: "mssql server"  
tags: ["sql server"]  
reading_time: 2 minutes  

---

# How to retrieve all row from all tables with where condition

I have a [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) and 10 [tables](https://www.mindstick.com/articles/336597/introduction-of-html-tables-for-web-development) in some tables I have a bit column [named](https://answers.mindstick.com/qa/44918/what-is-a-no-day-yet-named-motion) Open where I [store](https://www.mindstick.com/articles/13125/tips-to-increase-sales-of-your-woocommerce-store) a 0 if the record is not in use by an [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) and an 1 if the record is in use.

Well, I need to get all the [records](https://www.mindstick.com/forum/34640/how-to-create-a-stored-procedure-for-display-all-records) from all the tables in my database where the "open" [column value](https://www.mindstick.com/forum/161178/how-to-delete-duplicate-rows-based-on-single-column-value-in-mssql) is [true](https://yourviews.mindstick.com/view/81326/boycott-chinese-products-dream-will-come-true), or 1.

Is this even possible to do?

## Replies

### Reply by Anonymous User

Quick piece of code that gets list of tables within your database. Using a cursor loop through the answers checking it they have the fld named [open] and if it does the build a SQL statement and the execute this SQL string.

```
CREATE PROCEDURE usp_BulkTableOpenReport ASBEGIN     DECLARE @TBLS AS TABLE (REF INT IDENTITY (0,1), TABLENAME NVARCHAR(100), TABLEID BIGINT);    DECLARE @TBL AS NVARCHAR(100);    DECLARE @TBLID AS BIGINT;    DECLARE @SQL AS NVARCHAR(MAX);    DECLARE @I INT = 0;    DECLARE @M INT = 0;    DECLARE @V INT = 0     INSERT INTO @TBLS(TABLENAME,TABLEID)    SELECT NAME,OBJECT_ID FROM sys.tables     SELECT @M = MAX(REF) FROM @TBLS     WHILE @I <= @M    BEGIN        SELECT @TBL = TABLENAME, @TBLID= TABLEID FROM @TBLS WHERE REF = @I         /* CHECK TO MAKE INSURE THAT A FLD CALLED [OPEN] EXIST. */        SELECT @V = COUNT(*) FROM SYS.columns WHERE name = 'OPEN' AND  OBJECT_ID = @TBLID        IF @V != 0         BEGIN            SET @SQL = 'SELECT * FROM [' + @TBL + '] WHERE [OPEN] = 1'            EXEC SP_EXECUTESQL @SQL        END;        SET @I = @I + 1    END; ENDGO
```

From your c# application exec the query "EXEC usp_BulkTableOpenReport" then loop through the table outputs.


---

Original Source: https://www.mindstick.com/forum/23259/how-to-retrieve-all-row-from-all-tables-with-where-condition

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
