We'll get to know the techniques:
1. List all existing columns of the tables (INFORMATION_SCHEMA. COLUMNS)
:
To list all columns from all tables in the database:
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA. COLUMNS
2. Filter columns by data type (WHERE, DATA_TYPE, IN)
:
To list columns of a specific data type:
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA. COLUMNS
WHERE DATA_TYPE IN ('int', 'varchar')
3. Get
minimum and maximum column values (MIN, MAX):
4. Convert SQL result to XML format (FOR XML, RAW, AUTO)
:
To convert results of a query to XML:
SELECT Column1, Column2
FROM Table
FOR XML RAW, ELEMENTS
5. Declare Text Type Variable (DECLARE,
VARCHAR):
6. Concatenate Table Values to Variable (@VARIAVEL)
:
7. Create
Dynamic SQL Query:
8. Ensure that the text-type variable is in the valid format for the
object (QUOTENAME, IDENTIFIER):
9. Get all
characters except the last character (RIGHT, LEN) from the text type
variable:To get all characters except the last one from a text variable:
SET @MinhaVariavel = RIGHT(@MinhaVariavel, LEN(@MinhaVariavel) - 1)
10. Manipulate XML structure data (NODES, VALUE)
:
To manipulate data in an XML:
SELECT structure
XmlColumn.value('(Path/To/Node)[1]', 'varchar(50)') AS Value
FROM Table
11. Automatically create table with query result (INTO, FROM)
:
To create a table based on the result of a query:
SELECT Column1, Column2
INTO NewTable
FROM ExistingTable
12. Run Dynamic SQL Query (
EXEC):
Remember to tailor the examples to the specific structure of your database and the needs of your query.