Sounds like they want the ability to return only allowed fields, which means the number of fields returned also has to be dynamic. This will work with 2 variables. Anything more than that will be getting confusing.
IF (selectField1 = true AND selectField2 = true) BEGIN SELECT Field1, Field2 FROM Table END ELSE IF (selectField1 = true) BEGIN SELECT Field1 FROM Table END ELSE IF (selectField2 = true) BEGIN SELECT Field2 FROM Table END
Dynamic SQL will help with multiples. This examples is assuming atleast 1 column is true.
DECLARE @sql varchar(MAX) SET @sql = 'SELECT ' IF (selectField1 = true) BEGIN SET @sql = @sql + 'Field1, ' END IF (selectField2 = true) BEGIN SET @sql = @sql + 'Field2, ' END ... -- DROP ', ' @sql = SUBSTRING(@sql, 1, LEN(@sql)-2) SET @sql = @sql + ' FROM Table' EXEC(@sql)
Related Posts:
- How do I UPDATE from a SELECT in SQL Server?
- SQL SELECT WHERE field contains words
- SQL SELECT WHERE field contains words
- How do I use properly CASE..WHEN in MySQL
- SQL SELECT WHERE field contains words
- ORA-01843 not a valid month- Comparing Dates
- Best way to do nested case statement logic in SQL Server
- How to write a SQL DELETE statement with a SELECT statement in the WHERE clause?
- SQL: Two select statements in one query
- DATEDIFF function in Oracle
- MySQL error: Unknown column in ‘where clause’
- Postgresql column reference “id” is ambiguous
- How to order by column A and then by column B?
- Help me SELECT thumbnail from SQL and use
- how to SELECT meta values that are not null?
- What is the difference between “INNER JOIN” and “OUTER JOIN”?
- Case in Select Statement
- Unknown column in ‘field list’ error on MySQL Update query
- SQL Inner-join with 3 tables?
- Self Join to get employee manager name
- MySQL create table if not exists and insert record only if table was created
- How can I do an UPDATE statement with JOIN in SQL Server?
- ORA-00907: missing right parenthesis
- Rename column SQL Server 2008
- INSERT statement conflicted with the FOREIGN KEY constraint – SQL Server
- Case statement in MySQL
- Teradata: how to convert varchar value (format ‘dd.mm.yyyy’) to date (format ‘yyyy-mm-dd’ )?
- How Stuff and ‘For Xml Path’ work in SQL Server?
- Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
- How do I limit the number of rows returned by an Oracle query after ordering?
- Oracle SELECT TOP 10 records
- MySQL: Invalid use of group function
- must appear in the GROUP BY clause or be used in an aggregate function
- How can I return pivot table output in MySQL?
- The multi-part identifier could not be bound
- MySQL syntax for Join Update
- Get day of week in SQL Server 2005/2008
- incorrect syntax error CREATE VIEW must be the only statement in the batch
- PLS-00103: Encountered the symbol when expecting one of the following:
- SQLite – UPSERT *not* INSERT or REPLACE
- how to drop partition without dropping data in MySQL?
- Simple way to calculate median with MySQL
- The ALTER TABLE statement conflicted with the FOREIGN KEY constraint
- Error 1046 No database Selected, how to resolve?
- What is it exactly a BLOB in a DBMS context
- How do you create a temporary table in an Oracle database?
- Oracle SQL: Update a table with data from another table
- Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?
- Postgresql tables exists, but getting “relation does not exist” when querying
- SELECT DISTINCT on one column
- TSQL PIVOT MULTIPLE COLUMNS
- MySQL – UPDATE multiple rows with different values in one query
- How to create a MySQL hierarchical recursive query?
- Microsoft OLE DB Provider for SQL Server error ‘80004005’
- SQL Parentheses use in an OR clause
- Is it possible to GROUP BY multiple columns using MySQL?
- What is the difference between LATERAL JOIN and a subquery in PostgreSQL?
- Equivalent of Oracle’s RowID in SQL Server
- Is there a Max function in SQL Server that takes two values like Math.Max in .NET?
- Best way to test if a row exists in a MySQL table
- The used SELECT statements have a different number of columns
- Get current year in TSQL
- Is it possible to specify condition in Count()?
- SQL Server: Make all UPPER case to Proper Case/Title Case
- How do I query for all dates greater than a certain date in SQL Server?
- Conditional JOIN Statement SQL Server
- Temporary table in SQL server causing ‘ There is already an object named’ error
- Oracle: If Table Exists
- Equivalent of explode() to work with strings in MySQL
- How to calculate age (in years) based on Date of Birth and getDate()
- How to create id with AUTO_INCREMENT on Oracle?
- MySQL OPTIMIZE all tables?
- MySQL INSERT INTO table VALUES.. vs INSERT INTO table SET
- Text was truncated or one or more characters had no match in the target code page including the primary key in an unpivot
- How can I group by date time column without taking time into consideration
- How do I count unique items in field in Access query?
- Sqlite: CURRENT_TIMESTAMP is in GMT, not the timezone of the machine
- Append table to an existing one: SQL Server
- SQL Server – An expression of non-boolean type specified in a context where a condition is expected, near ‘RETURN’
- Update query using Subquery in Sql Server
- What is a postgres superuser
- “This SqlTransaction has completed; it is no longer usable.”… configuration error?
- register_post_status – show_in_admin_all_list & show_in_admin_status_list does not affect query
- Return number of items in a table with post id
- WP Optimization: Removing Orphaned wp_options (especially the autoload ones)
- Auto-update tables from database
- Debugging WordPress
- dbDelta not creating table (yes, once more)
- Pull data and display as rows and columns
- I need a SQL command to delete specific authors and all posts connected with them
- How can i restore only Blogs from a SQL backup file?
- DB_HOST – is LOCALHOST speedy than domain name? [closed]
- How to export 2 week’s worth of posts
- How to change post published date to today date with SQL
- Do I need to sanitize $_POST[‘keyword’] before send to ‘s’ parameter?
- Nested select statements not working
- Sql query returns empty. But not
- SQL – JOIN last child
- wpdb prepare without placeholder
- Adding profile data to database