using …

Pure C#

‘Cross-Tabs’ Kategorisi için Arşiv

SQL Server / T-SQL Tutorial

Yazan: esersahin 20/03/2009

http://www.java2s.com/Tutorial/SQLServer/CatalogSQLServer.htm

1. Query
1. 1. Query Select( 18 ) 1. 17. ANY( 6 )
1. 2. where( 10 ) 1. 18. Some( 2 )
1. 3. Order by( 16 ) 1. 19. AND( 2 )
1. 4. GROUP BY( 8 ) 1. 20. OR( 4 )
1. 5. Having( 7 ) 1. 21. Not( 6 )
1. 6. Null( 2 ) 1. 22. AS( 2 )
1. 7. IS NOT NULL( 1 ) 1. 23. Case( 19 )
1. 8. IS NULL( 3 ) 1. 24. arithmetic operator( 1 )
1. 9. Between( 8 ) 1. 25. Math Calculation( 4 )
1. 10. Exists( 6 ) 1. 26. Logical operator( 11 )
1. 11. In( 7 ) 1. 27. Comparison operators( 7 )
1. 12. DISTINCT( 7 ) 1. 28. Query Hints( 2 )
1. 13. LIKE( 16 ) 1. 29. Select Into( 6 )
1. 14. Column Alias( 5 ) 1. 30. TABLESAMPLE( 1 )
1. 15. Top( 10 ) 1. 31. ROWGUIDCOL( 1 )
1. 16. ALL( 8 ) 1. 32. rowversion( 1 )
2. Insert Delete Update
2. 1. Insert( 12 ) 2. 4. Delete( 9 )
2. 2. Insert into select( 5 ) 2. 5. Update( 16 )
2. 3. BULK INSERT( 1 ) 2. 6. Output( 1 )
3. Table
3. 1. Create Table( 6 ) 3. 5. DROP TABLE( 2 )
3. 2. ALTER TABLE( 16 ) 3. 6. Table alias( 5 )
3. 3. Column( 7 ) 3. 7. Temporary Table( 9 )
3. 4. Computed Columns( 1 ) 3. 8. TRUNCATE table( 2 )
4. Table Join
4. 1. Table Join( 14 ) 4. 7. Self join( 3 )
4. 2. Cross Join( 4 ) 4. 8. MERGE( 1 )
4. 3. Inner join( 8 ) 4. 9. One to many( 2 )
4. 4. Left Join( 1 ) 4. 10. many to many( 2 )
4. 5. LEFT OUTER JOIN( 5 ) 4. 11. APPLY( 1 )
4. 6. RIGHT OUTER JOIN( 2 )
5. Data Types
5. 1. bigint( 1 ) 5. 16. nchar( 1 )
5. 2. bit( 4 ) 5. 17. nVarChar( 2 )
5. 3. bitwise operators( 13 ) 5. 18. OPENROWSET( 2 )
5. 4. Char( 4 ) 5. 19. READTEXT( 1 )
5. 5. collate( 2 ) 5. 20. smalldatetime( 2 )
5. 6. Create Type( 5 ) 5. 21. Smallint( 1 )
5. 7. Data type( 1 ) 5. 22. SQL_VARIANT( 6 )
5. 8. Date Type( 1 ) 5. 23. text( 2 )
5. 9. datetime( 18 ) 5. 24. TEXTPTR( 1 )
5. 10. decimal( 4 ) 5. 25. timestamp( 2 )
5. 11. Float( 1 ) 5. 26. VARBINARY( 4 )
5. 12. FULLTEXT( 8 ) 5. 27. VARCHAR( 13 )
5. 13. integer( 9 ) 5. 28. WRITETEXT( 1 )
5. 14. Large Text( 3 ) 5. 29. Unicode( 1 )
5. 15. money( 1 )
6. Set Operations
6. 1. EXCEPT( 2 ) 6. 3. Union( 6 )
6. 2. INTERSECT( 2 )
7. Constraints
7. 1. Constraints( 5 ) 7. 7. Create Default( 2 )
7. 2. Primary Key( 7 ) 7. 8. Create Rule( 2 )
7. 3. Foreign Key( 9 ) 7. 9. NewID( 1 )
7. 4. REFERENCES( 5 ) 7. 10. Unique( 7 )
7. 5. Check( 13 ) 7. 11. uniqueidentifier( 1 )
7. 6. Default( 8 ) 7. 12. Constriant violation( 4 )
8. Subquery
8. 1. Subquery( 16 ) 8. 3. Inline view( 10 )
8. 2. Correlated subquery( 4 )
9. Aggregate Functions
9. 1. Aggregate function( 5 ) 9. 5. Min( 6 )
9. 2. AVG( 6 ) 9. 6. RANK( 3 )
9. 3. Count( 8 ) 9. 7. SUM( 6 )
9. 4. Max( 6 )
10. Date Functions
10. 1. CURRENT_TIMESTAMP( 4 ) 10. 8. DATEPART( 25 )
10. 2. Date calculation( 1 ) 10. 9. Day( 3 )
10. 3. DATEADD( 16 ) 10. 10. GETDATE( 7 )
10. 4. DATEDIFF( 16 ) 10. 11. GETUTCDATE( 1 )
10. 5. DATEFIRST( 3 ) 10. 12. ISDATE( 4 )
10. 6. DATEFORMAT( 1 ) 10. 13. MONTH( 2 )
10. 7. DATENAME( 15 ) 10. 14. Year( 2 )
11. Math Functions
11. 1. ABS( 2 ) 11. 13. LOG10( 1 )
11. 2. ACOS( 1 ) 11. 14. PI( 1 )
11. 3. ASIN( 1 ) 11. 15. Power( 2 )
11. 4. ATAN( 1 ) 11. 16. RADIANS( 1 )
11. 5. CEILING( 4 ) 11. 17. RAND( 4 )
11. 6. COS( 1 ) 11. 18. ROUND( 9 )
11. 7. COT( 1 ) 11. 19. SIGN( 1 )
11. 8. DEGREES( 1 ) 11. 20. Sin( 2 )
11. 9. EXP( 1 ) 11. 21. SQRT( 3 )
11. 10. FLOOR( 7 ) 11. 22. SQUARE( 2 )
11. 11. ISNUMERIC( 5 ) 11. 23. TAN( 1 )
11. 12. LOG( 1 )
12. String Functions
12. 1. ASCII( 3 ) 12. 13. REPLACE( 1 )
12. 2. Char function( 4 ) 12. 14. REPLICATE( 5 )
12. 3. CHARINDEX( 6 ) 12. 15. REVERSE( 2 )
12. 4. DIFFERENCE( 7 ) 12. 16. Right( 4 )
12. 5. FORMATMESSAGE( 1 ) 12. 17. RTRIM( 6 )
12. 6. LEFT( 7 ) 12. 18. SOUNDEX( 4 )
12. 7. LEN( 4 ) 12. 19. SPACE( 2 )
12. 8. LOWER( 4 ) 12. 20. STR( 6 )
12. 9. LTRIM( 4 ) 12. 21. STUFF( 3 )
12. 10. nchar function( 3 ) 12. 22. SUBSTRING( 8 )
12. 11. PATINDEX( 5 ) 12. 23. UNICODE function( 2 )
12. 12. QUOTENAME( 2 ) 12. 24. UPPER( 4 )
13. Data Convert Functions
13. 1. CAST( 7 ) 13. 4. DROP DATABASE( 2 )
13. 2. COALESCE( 5 ) 13. 5. ISNULL( 7 )
13. 3. CONVERT( 26 ) 13. 6. NULLIF( 1 )
14. Analytical Functions
14. 1. COMPUTE( 5 ) 14. 8. ROLLUP( 8 )
14. 2. CUBE( 7 ) 14. 9. ROW_NUMBER( 2 )
14. 3. DENSE_RANK( 1 ) 14. 10. STDEV( 3 )
14. 4. GROUPING( 4 ) 14. 11. STDEVP( 2 )
14. 5. NTILE( 1 ) 14. 12. VAR( 2 )
14. 6. PARTITION( 3 ) 14. 13. VARP( 1 )
14. 7. PIVOT( 1 )
15. Sequence Indentity
15. 1. Identity( 22 ) 15. 5. IDENT_INCR( 2 )
15. 2. IDENTITYCOL( 2 ) 15. 6. IDENT_SEED( 2 )
15. 3. IDENTITY_INSERT( 2 ) 15. 7. SCOPE_IDENTITY( 3 )
15. 4. IDENT_CURRENT( 2 )
16. View
16. 1. View( 8 ) 16. 5. DROP VIEW( 3 )
16. 2. Create View( 10 ) 16. 6. Filter view( 5 )
16. 3. Query view( 1 ) 16. 7. Updatable View( 6 )
16. 4. ALTER VIEW( 2 )
17. Index
17. 1. CREATE INDEX( 18 ) 17. 2. DROP INDEX( 2 )
18. Cursor
18. 1. Cursor( 3 ) 18. 5. FETCH_STATUS( 1 )
18. 2. Declare Cursor( 12 ) 18. 6. Close cursor( 1 )
18. 3. Open Cursor( 1 ) 18. 7. DEALLOCATE cursor( 2 )
18. 4. Fetch cursor( 3 ) 18. 8. cursor_status( 1 )
19. Database
19. 1. Create Database( 9 ) 19. 4. BACKUP( 3 )
19. 2. Use( 1 ) 19. 5. RESTORE DATABASE( 2 )
19. 3. Alter Database( 8 )
20. Transact SQL
20. 1. Declare( 7 ) 20. 14. Delete Statement( 5 )
20. 2. Variable( 15 ) 20. 15. Insert Statement( 5 )
20. 3. Variable Select( 8 ) 20. 16. Select statement( 4 )
20. 4. Set( 7 ) 20. 17. Update Statement( 2 )
20. 5. Table Variable( 5 ) 20. 18. Dynamic SQL( 8 )
20. 6. Print( 5 ) 20. 19. EXEC( 12 )
20. 7. IF( 14 ) 20. 20. Error Handler( 1 )
20. 8. while( 10 ) 20. 21. RAISERROR( 9 )
20. 9. Goto( 3 ) 20. 22. Return( 7 )
20. 10. Break( 1 ) 20. 23. Try catch( 7 )
20. 11. Comments( 2 ) 20. 24. WAITFOR( 4 )
20. 12. Continue( 1 ) 20. 25. With( 4 )
20. 13. Code Block( 2 )
21. Procedure Function
21. 1. Create function( 6 ) 21. 9. Procedure call( 2 )
21. 2. function( 19 ) 21. 10. Alter procedure( 4 )
21. 3. Recursive function( 5 ) 21. 11. Utility Procedure( 5 )
21. 4. Alter function( 1 ) 21. 12. Drop procedure( 2 )
21. 5. Drop function( 2 ) 21. 13. Parameter( 20 )
21. 6. Utility function( 6 ) 21. 14. Output parameter( 3 )
21. 7. Create Procedure( 6 ) 21. 15. Returning value( 4 )
21. 8. Procedure( 10 )
22. Trigger
22. 1. Trigger( 14 ) 22. 10. Trigger order( 2 )
22. 2. Alter Trigger( 4 ) 22. 11. Drop trigger( 2 )
22. 3. Trigger for after( 4 ) 22. 12. COLUMNS_UPDATED( 1 )
22. 4. Trigger for Delete( 4 ) 22. 13. Update function( 3 )
22. 5. Trigger for insert( 1 ) 22. 14. Deleted table( 2 )
22. 6. Trigger for update( 4 ) 22. 15. Inserted table( 5 )
22. 7. Trigger on database( 2 ) 22. 16. RECURSIVE_TRIGGERS( 1 )
22. 8. Trigger on server( 1 ) 22. 17. Utility trigger( 4 )
22. 9. Trigger on view( 3 )
23. Transaction
23. 1. TRANSACTION( 14 ) 23. 4. Save points( 2 )
23. 2. TRANSACTION ISOLATION LEVEL( 3 ) 23. 5. TRANCOUNT( 2 )
23. 3. COMMIT( 1 )
24. XML
24. 1. XML( 7 ) 24. 4. XML query( 5 )
24. 2. For XML( 4 ) 24. 5. XML Schema( 1 )
24. 3. XML Index( 1 )
25. System Functions
25. 1. APP_NAME( 1 ) 25. 31. sp_depends( 1 )
25. 2. COL_LENGTH( 2 ) 25. 32. sp_describe_cursor( 1 )
25. 3. COL_NAME( 1 ) 25. 33. sp_dropdevice( 1 )
25. 4. context_info( 1 ) 25. 34. sp_DropLogin( 1 )
25. 5. DATABASEPROPERTY( 1 ) 25. 35. sp_dropmessage( 1 )
25. 6. DATABASEPROPERTYEX( 1 ) 25. 36. sp_DropRoleMember( 3 )
25. 7. DATALENGTH( 4 ) 25. 37. sp_DropSrvRoleMember( 2 )
25. 8. db_name( 2 ) 25. 38. sp_droptype( 1 )
25. 9. DecryptByPassphrase( 1 ) 25. 39. sp_GrantDbAccess( 2 )
25. 10. EncryptByPassPhrase( 1 ) 25. 40. sp_GrantLogin( 1 )
25. 11. fn_builtin_permissions( 1 ) 25. 41. sp_help( 3 )
25. 12. fn_helpcollations( 1 ) 25. 42. sp_helpconstraint( 2 )
25. 13. fn_varbintohexstr( 1 ) 25. 43. sp_helpdb( 2 )
25. 14. fn_varbintohexsubstring( 1 ) 25. 44. sp_helpindex( 1 )
25. 15. fn_virtualfilestats( 1 ) 25. 45. sp_HelpRole( 2 )
25. 16. sp_addlinkedserver( 1 ) 25. 46. sp_helptext( 4 )
25. 17. sp_addlogin( 2 ) 25. 47. sp_helpuser( 1 )
25. 18. sp_addmessage( 3 ) 25. 48. sp_makestartup( 1 )
25. 19. sp_AddRole( 2 ) 25. 49. sp_monitor( 1 )
25. 20. sp_addrolemember( 5 ) 25. 50. sp_procoption( 1 )
25. 21. sp_AddSrvRoleMember( 2 ) 25. 51. sp_recompile( 2 )
25. 22. sp_addtype( 3 ) 25. 52. sp_refreshview( 1 )
25. 23. sp_altermessage( 1 ) 25. 53. sp_rename( 2 )
25. 24. sp_bindefault( 2 ) 25. 54. sp_RevokeDbAccess( 2 )
25. 25. sp_bindrule( 1 ) 25. 55. sp_serveroption( 1 )
25. 26. sp_change_users_login( 1 ) 25. 56. sp_spaceused( 2 )
25. 27. sp_configure( 4 ) 25. 57. sp_unbindefault( 1 )
25. 28. sp_create_removable( 1 ) 25. 58. sp_unmakestartup( 1 )
25. 29. sp_dboption( 9 ) 25. 59. SYSTEM_USER( 3 )
25. 30. sp_DenyLogin( 1 )
26. System Settings
26. 1. ANSI_NULLS( 1 ) 26. 24. PACK_RECEIVED( 1 )
26. 2. CONNECTIONS( 1 ) 26. 25. PACK_SENT( 1 )
26. 3. CPU_BUSY( 1 ) 26. 26. QUOTED_IDENTIFIER( 1 )
26. 4. DBCC( 10 ) 26. 27. rowcount( 7 )
26. 5. DB_ID( 1 ) 26. 28. ROWCOUNT_BIG( 1 )
26. 6. ERROR( 11 ) 26. 29. SERVERNAME( 1 )
26. 7. ERROR_NUMBER( 1 ) 26. 30. SERVERPROPERTY( 2 )
26. 8. ERROR_SEVERITY( 1 ) 26. 31. SERVICENAME( 1 )
26. 9. FORCEPLAN( 1 ) 26. 32. SESSIONPROPERTY( 7 )
26. 10. HashBytes( 1 ) 26. 33. SESSION_USER( 1 )
26. 11. HOST_ID( 1 ) 26. 34. SHOWPLAN_TEXT( 2 )
26. 12. HOST_NAME( 1 ) 26. 35. SPID( 1 )
26. 13. IDLE( 1 ) 26. 36. SUSER_ID( 1 )
26. 14. Information_schema( 10 ) 26. 37. SUSER_NAME( 1 )
26. 15. IO_BUSY( 1 ) 26. 38. TIMETICKS( 1 )
26. 16. LANGID( 1 ) 26. 39. TOTAL_ERRORS( 1 )
26. 17. LOCK_TIMEOUT( 2 ) 26. 40. TOTAL_READ( 1 )
26. 18. NESTLEVEL( 1 ) 26. 41. TOTAL_WRITE( 1 )
26. 19. NOCOUNT( 1 ) 26. 42. VERSION( 2 )
26. 20. OBJECTPROPERTY( 4 ) 26. 43. XACT_ABORT( 3 )
26. 21. OBJECT_ID( 5 ) 26. 44. xp_cmdshell( 2 )
26. 22. OBJECT_NAME( 1 ) 26. 45. xp_msver( 2 )
26. 23. PACKET_ERRORS( 1 ) 26. 46. xp_sendmail( 1 )
27. System Tables Views
27. 1. sys.columns( 1 ) 27. 19. sys.triggers( 3 )
27. 2. sys.databases( 3 ) 27. 20. sys.types( 1 )
27. 3. sys.database_principals( 2 ) 27. 21. sys.views( 3 )
27. 4. sys.fn_varbintohexstr( 1 ) 27. 22. sys.xml_schema_collections( 1 )
27. 5. sys.fulltext_catalogs( 1 ) 27. 23. sys.xml_schema_namespaces( 1 )
27. 6. sys.fulltext_indexes( 1 ) 27. 24. syscolumns( 2 )
27. 7. sys.fulltext_index_columns( 1 ) 27. 25. syscomments( 3 )
27. 8. sys.indexes( 1 ) 27. 26. sysconstraints( 2 )
27. 9. sys.messages( 1 ) 27. 27. sysdatabases( 2 )
27. 10. sys.objects( 4 ) 27. 28. sysdepends( 2 )
27. 11. sys.schemas( 3 ) 27. 29. sysfiles( 1 )
27. 12. sys.server_principals( 1 ) 27. 30. sysindexes( 3 )
27. 13. sys.server_sql_modules( 1 ) 27. 31. syslogins( 1 )
27. 14. sys.server_triggers( 1 ) 27. 32. sysmessages( 1 )
27. 15. sys.sql_dependencies( 1 ) 27. 33. sysobjects( 5 )
27. 16. sys.sql_modules( 2 ) 27. 34. systypes ( 1 )
27. 17. sys.syscacheobjects( 1 ) 27. 35. SYSUSERS( 2 )
27. 18. sys.syslogins( 1 )
28. User Role
28. 1. CREATE CERTIFICATE( 2 ) 28. 8. REVOKE( 6 )
28. 2. CREATE SCHEMA( 1 ) 28. 9. Role( 11 )
28. 3. CURRENT_USER( 1 ) 28. 10. schema( 3 )
28. 4. ALTER AUTHORIZATION( 1 ) 28. 11. USER( 8 )
28. 5. DENY( 2 ) 28. 12. USER_ID( 1 )
28. 6. GRANT( 14 ) 28. 13. USER_NAME( 1 )
28. 7. LOGIN( 13 )
29. CLR
29. 1. ASSEMBLY( 4 ) 29. 2. CLR( 6 )

Yazı kategorisi: CTE, Cast, Common Table Expressions, Cross-Tabs, Functions, Instead Of, Pivot Tables, ROW_NUMBER, Raiserror, SQL, SQLCLR, Sql Server, Sql Server Error, Sql Server Join, Stored Procedure, Transact-SQL, Trigger, UNION, UNION ALL, Update with Join | » yorum bırak;

SQL Server / T-SQL

Yazan: esersahin 20/03/2009

http://www.java2s.com/Code/SQLServer/CatalogSQLServer.htm

Select Query Subquery Insert Delete Update Data Set
Select Query
ALL( 1 ) AND( 1 ) ANY( 1 ) AS( 3 ) Between AND( 5 ) Bitwise Operator( 1 )
Case( 9 ) Column Alias( 4 ) Compare Date( 2 ) Data Type( 1 ) Date Calculation( 1 ) Date Compare( 1 )
DESC( 1 ) DISTINCT( 3 ) EXISTS( 5 ) Group BY( 9 ) HAVING( 6 ) IN( 6 )
IS NULL( 2 ) ISNULL( 6 ) LIKE( 10 ) Logical Operator( 7 ) Math Operator( 2 ) MAXRECURSION( 1 )
NOT( 4 ) NULL( 3 ) Operator Precedence( 2 ) OR( 2 ) Order Columns( 5 ) Order( 11 )
Query Batch( 1 ) Query Columns( 6 ) Quotation Mark( 1 ) Select Into( 3 ) Select( 13 ) String Compare( 1 )
Top percent( 4 ) Top With Ties( 1 ) Top( 7 ) Where( 9 ) With( 2 )
Subquery
Correlated subquery( 6 ) Nested Subquery( 2 ) RTRIM( 4 ) Subqueries ALL( 1 ) Subqueries ANY( 2 ) Subqueries Exists( 4 )
Subqueries IN( 5 ) Subqueries( 4 ) Subquery Aggregate Functions( 2 ) Subquery From( 1 )
Insert Delete Update
Batch Insert( 2 ) BULK INSERT( 1 ) Delete( 6 ) Insert Image( 1 ) Insert Select( 4 ) Insert( 5 )
Update( 15 )
Data Set
EXCEPT( 1 ) INTERSECT( 1 ) UNION ALL( 1 ) Union( 3 )
Store Procedure Function Transact SQL Trigger
Store Procedure Function
Create Function( 5 ) Create Procedure( 13 ) Default Parameter( 3 ) Drop Procedure( 1 ) EXEC( 6 ) EXECUTE( 2 )
Function Call( 2 ) Function Parameters( 1 ) Function Return( 7 ) Procedure Call( 3 ) Procedure Parameters( 2 ) Recursive function( 1 )
Scalar Functions( 1 )
Transact SQL
Assign Value( 4 ) ATAN( 1 ) Break( 2 ) COALESCE( 1 ) Code Block( 2 ) Continue( 2 )
Declare Variables( 5 ) Delete Data( 1 ) Dynamic SQL( 4 ) Error( 4 ) GOTO( 1 ) Handle Error( 1 )
If( 7 ) Insert Data( 6 ) LOG( 2 ) Object Property( 1 ) Print( 1 ) Query Data( 2 )
RAISERROR( 2 ) Return( 2 ) RowCount( 3 ) schemas( 1 ) Select Data( 1 ) Select Variables( 2 )
Set Variables( 4 ) Statement( 1 ) Table Variable( 5 ) Update Data( 5 ) Variable Scope( 2 ) Variables( 1 )
Version( 1 ) WAITFOR( 2 ) While( 5 )
Trigger
Alter Trigger( 1 ) Create Trigger( 3 ) Disable Trigger( 1 ) Enable Disable Trigger( 1 ) inserted table( 1 ) INSTEAD OF Trigger( 3 )
Trigger after( 2 ) Trigger for Insert( 1 ) Trigger for update( 4 ) Trigger Value( 2 ) UPDATE function( 1 )
Table Table Joins View Index Database
Table
Add Column( 3 ) Alter Table( 2 ) Column Data Type( 1 ) Column Index( 1 ) Computed Column( 2 ) Copy Data( 1 )
Create Table( 1 ) Derived Columns( 1 ) Drop Column( 2 ) Drop Table( 3 ) Rename Column( 1 ) Rename Table( 1 )
Sub query( 2 ) Table Alias( 1 ) Table Copy( 1 ) Table Existence( 1 ) Table Properties( 1 ) Temporary Table( 3 )
TRUNCATE TABLE( 2 )
Table Joins
ANSI Join Syntax( 3 ) Cross Join( 3 ) INNER JOIN( 4 ) Left Join( 2 ) LEFT OUTER JOIN( 1 ) RIGHT JOIN( 1 )
RIGHT OUTER JOIN( 2 ) Self Join( 1 ) SQL Server Join Syntax( 2 ) Table Join( 10 )
View
Alter View( 2 ) Create View( 13 ) Drop View( 2 ) ENCRYPTION( 1 ) Filter view( 2 ) Inline view( 2 )
Nested View( 1 ) Query View( 1 ) SCHEMABINDING( 1 ) Updatable View( 2 ) View Existance( 1 )
Index
CLUSTERED INDEX( 2 ) Create Index( 6 ) Drop Index( 1 ) DROP_EXISTING( 1 )
Database
Create Database( 3 ) Database Info( 1 ) Database Option( 7 ) databases( 2 ) DROP DATABASE( 2 ) USE( 1 )
Transaction Cursor Constraints Sequence
Transaction
Commit Transaction( 3 ) Transaction Roll back( 4 ) XACT_ABORT( 1 )
Cursor
Declare CURSOR( 3 ) Fetch Data( 2 ) Fetch_Status( 1 ) Open cursor( 2 )
Constraints
Add Primary Key( 2 ) CASCADE( 1 ) Check Options( 6 ) Check value range( 3 ) Column Constraints( 5 ) Constraints Basics( 2 )
CREATE RULE( 1 ) Default Value( 3 ) Disable constraint( 1 ) Foreign Key( 4 ) Maintain Constaints( 1 ) Nullable( 3 )
Primary key( 3 ) Unique( 3 ) UniqueIdentifier( 1 )
Sequence
IDENTITY( 12 ) IDENTITYCOL( 1 ) IDENT_CURRENT( 2 ) NEWID( 1 ) Procedure Call( 3 ) Procedure Existance( 1 )
Procedure Permission( 1 ) Procedure with Parameters( 13 ) Procedure( 1 )
Data Type XML Date Timezone
Data Type
Bit( 2 ) Cast( 21 ) char( 1 ) Convert( 8 ) Data Type Convert( 1 ) datetime( 1 )
decimal( 7 ) float( 1 ) int( 3 ) Money( 1 ) nchar Data Type( 1 ) Number Format( 2 )
text( 1 ) varchar( 3 )
XML
XML column( 2 )
Date Timezone
CURRENT_TIMESTAMP( 1 ) Date Data Type( 2 ) Date Format( 12 ) DATEADD( 18 ) DATEDIFF( 19 ) DATENAME( 14 )
DATEPART( 13 ) Datetime( 3 ) Day( 4 ) GetDate( 4 ) GETUTCDATE( 2 ) Int( 1 )
ISDATE( 6 ) Month( 4 ) Year( 4 )
Aggregate Functions Analytical Functions Math Functions String Functions
Aggregate Functions
Aggregate Function Basics( 2 ) AVG( 4 ) Count( 6 ) COUNT_BIG( 2 ) MAX( 3 ) MIN( 2 )
SUM( 2 )
Analytical Functions
COMPUTE( 5 ) Cube( 2 ) DENSE RANK( 2 ) NTile( 1 ) Over( 1 ) RANK( 2 )
Row Number( 3 ) STDEV( 4 ) VAR( 3 ) VARP( 2 ) WITH ROLLUP( 2 )
Math Functions
ABS( 2 ) ACOS( 2 ) ASIN( 1 ) CEILING( 3 ) COS( 2 ) COT( 1 )
DEGREES( 2 ) EXP( 1 ) FLOOR( 3 ) ISNUMERIC( 3 ) Log10( 2 ) mod( 1 )
PI( 1 ) Power( 2 ) RADIANS( 1 ) RAND( 1 ) ROUND( 9 ) SIGN( 1 )
SIN( 1 ) SQRT( 1 ) SQUARE( 1 ) TAN( 1 )
String Functions
ASCII( 2 ) CHAR( 3 ) CHARINDEX( 5 ) COLLATE( 1 ) DATALENGTH( 3 ) Difference( 4 )
Escape( 1 ) LEFT( 4 ) LEN( 4 ) Lower( 2 ) LTRIM( 2 ) NCHAR( 2 )
PATINDEX( 2 ) QUOTENAME( 1 ) Regular Expressions( 5 ) REPLACE( 3 ) REPLICATE( 4 ) REVERSE( 2 )
RIGHT( 4 ) SOUNDEX( 2 ) Space( 2 ) STR( 6 ) STUFF( 2 ) Substring( 4 )
UNICODE( 1 ) UPPER( 3 )
System
System
columns( 2 ) DATABASEPROPERTYEX( 1 ) DB_ID( 1 ) Error_Message( 1 ) HOST_NAME( 1 ) INFORMATION_SCHEMA( 3 )
LOCK_TIMEOUT( 1 ) LOGIN( 1 ) OBJECT_ID( 1 ) QUOTED_IDENTIFIER( 2 ) Row Count( 2 ) ROWCOUNT( 1 )
SHOWPLAN_TEXT( 2 ) sp_addmessage( 2 ) sp_altermessage( 1 ) sp_dboption( 1 ) sp_dropmessage( 1 ) sp_helpconstraint( 1 )
sp_helpindex( 1 ) sp_helptext( 1 ) sp_unbindefault( 1 ) STATISTICS( 1 ) SUSER_SNAME( 1 ) sys.sql_modules( 1 )
sys.syscacheobjects( 1 ) sys.types( 1 ) sys.views( 1 ) sys.XML_schema_collections( 1 ) syscomments( 1 ) sysdepends( 1 )
sysmessages( 2 ) sysobjects( 2 ) sysusers( 1 ) TOTAL_ERRORS( 1 ) TOTAL_READ( 1 ) USER( 1 )
USER_NAME( 1 ) views( 1 )

Yazı kategorisi: CTE, Cast, Cross-Tabs, Functions, Pivot Tables, SQL, Sql Server, Sql Server Error, Sql Server Join, Stored Procedure, Transact-SQL, Trigger | » yorum bırak;

Dynamic Cross-Tabs/Pivot Tables

Yazan: esersahin 24/11/2008

http://www.sqlteam.com/article/dynamic-cross-tabs-pivot-tables

IMHO, the best feature of MS Access is the TRANSFORM statement, used to create cross-tabs/pivot tables. It does all of the work of dynamically generating the cross-tabulation and the summary calculations. T-SQL unfortunately doesn’t have this statement, so you’re stuck using complicated SQL commands, expensive 3rd party products, or exotic OLAP to make pivot tables…or you can use the following procedure to dynamically create them!

<!–


–>

I got the idea from this question, asking how to “undo” a pivot table, and then I started working on how to create them in T-SQL. There are numerous ways of doing pivot tables, and this site has several examples (and lots of other cool stuff). The standard method uses a CASE statement, with one CASE for each pivot value (the column headings created by cross-tabbing the pivot column). The greatest shortcoming is finding a way to handle an unknown or changing number of pivot values. Obviously you have to know these values beforehand, and you must add a CASE for each new, distinct value inserted into the pivot column. The code listed below will do all of the work for you:

CREATE PROCEDURE crosstab
@select varchar(8000),
@sumfunc varchar(100),
@pivot varchar(100),
@table varchar(100)
AS

DECLARE @sql varchar(8000), @delim varchar(1)
SET NOCOUNT ON
SET ANSI_WARNINGS OFF

EXEC ('SELECT ' + @pivot + ' AS pivot INTO ##pivot FROM ' + @table + ' WHERE 1=2')
EXEC ('INSERT INTO ##pivot SELECT DISTINCT ' + @pivot + ' FROM ' + @table + ' WHERE '
+ @pivot + ' Is Not Null')

SELECT @sql='',  @sumfunc=stuff(@sumfunc, len(@sumfunc), 1, ' END)' )

SELECT @delim=CASE Sign( CharIndex('char', data_type)+CharIndex('date', data_type) )
WHEN 0 THEN '' ELSE '''' END
FROM tempdb.information_schema.columns
WHERE table_name='##pivot' AND column_name='pivot'

SELECT @sql=@sql + '''' + convert(varchar(100), pivot) + ''' = ' +
stuff(@sumfunc,charindex( '(', @sumfunc )+1, 0, ' CASE ' + @pivot + ' WHEN '
+ @delim + convert(varchar(100), pivot) + @delim + ' THEN ' ) + ', ' FROM ##pivot

DROP TABLE ##pivot

SELECT @sql=left(@sql, len(@sql)-1)
SELECT @select=stuff(@select, charindex(' FROM ', @select)+1, 0, ', ' + @sql + ' ')

EXEC (@select)
SET ANSI_WARNINGS ON

Yeah, I know, you want to know how it works :) I won’t go into detail on the mechanics of the code. Let’s just say that if you can follow it, feel free to play with it; if not, DON’T TOUCH IT! The syntax for the procedure call is below, followed by a description of each parameter:

  1 2 3 4
EXECUTE crosstab SELECT statement, summary calculation, pivot column, table name
  1. The SELECT statement can be anything, as long as it has proper syntax and includes a GROUP BY clause. You can use JOINs, but if you use table aliases you should include the alias in the summary calculation expression (2). 
  2. The summary calculation must have an aggregate function like SUM(), AVG(), MIN(), MAX(), etc. You’d have to modify the code if you want to use DISTINCT with these functions. COUNT(*) won’t work, you have to COUNT on a column. 
  3. The pivot column must be in the table (4). You can use an expression for the pivot column (a+b, LEFT(FirstName,3), etc.) as long as it can be derived from the table listed in (4). A cross-tab heading will be created for each distinct value in the pivot colum/expression. 
  4. This table can be any table in your database, or another database if you use the full naming syntax (database.owner.table). Tables in a linked server may also work, but I haven’t tested this. It’s possible that a derived table (nested SELECT) can work, but I haven’t tested this either. You would need to enclose the SELECT statement in parentheses, and use a table alias outside these parentheses, like this: ‘(SELECT LastName FROM myTable) AS Surnames’

I’ll list some cross-tab settings and the results. Here’s two you can run in the pubs database:

EXECUTE crosstab 'select title from titles inner join sales on (sales.title_id=titles.title_id)
group by title', 'sum(qty)','stor_id','stores'
title 6380 7066 7067 7131 7896 8042
But Is It User Friendly?           30
Computer Phobic AND Non-Phobic Individuals: Behavior Variations       20    
Cooking with Computers: Surreptitious Balance Sheets           25
Emotional Security: A New Algorithm       25    
Fifty Years in Buckingham Palace Kitchens     20      
Is Anger the Enemy? 3 75 10 20    
Life Without Fear       25    
Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean     40      
Prolonged Data Deprivation: Four Case Studies       15    
Secrets of Silicon Valley   50        
Silicon Valley Gastronomic Treats         10  
Straight Talk About Computers         15  
Sushi, Anyone?     20      
The Busy Executive’s Database Guide 5         10
The Gourmet Microwave       25   15
You Can Combat Computer Stress!         35  
EXECUTE crosstab 'select pub_name, count(qty) as orders, sum(qty) as total
from sales inner join titles on (sales.title_id=titles.title_id)
right join publishers on (publishers.pub_id=titles.pub_id)
group by pub_name', 'sum(qty)','type','titles'
pub_name orders total business mod_cook popular_comp psychology trad_cook UNDECIDED
Algodata Infosystems 6 135 55   80      
Binnet & Hardley 7 150   50   20 80  
Five Lakes Publishing 0              
GGG&G 0              
Lucerne Publishing 0              
New Moon Books 8 208 35     173    
Ramona Publishers 0              
Scootney Books 0              

Here’s one that will run in Northwind:

EXECUTE crosstab 'SELECT LastName FROM Employees INNER JOIN Orders
ON (Employees.EmployeeID=Orders.EmployeeID)
GROUP BY LastName', 'count(lastname)', 'Year(OrderDate)', 'Orders'
LastName 1996 1997 1998
Buchanan 11 18 13
Leverling 18 71 38
Fuller 16 41 39
Peacock 31 81 44
Callahan 19 54 31
Suyama 15 33 19
King 11 36 25
Dodsworth 5 19 19
Davolio 26 55 42

Some things to look out for:

  • This procedure builds a SQL statement from your parameters, inserts the CASE statements to handle the cross-tabbing, and then executes it. The size of this statement is maxed out at 8000 characters, so there’s a limit on the size and complexity of the SELECT statement and the number of pivoted values you can have. 
  • This procedure uses a global temporary table, and will cause an error if two sessions execute it at the same time. (thanks for catching this Garth!) I will have a remedy soon and will post the fixed code in this article. 
  • Date values can be pivoted, but remember that the timestamp portion will be included, so you should CONVERT() them in the pivot expression. NULLs can’t be pivoted, you must use IsNull() to replace them with a value. I have not tested nvarchar or nchar data types, and the code may need to be modified to work with them. Nor have I tried ntext or text types, but I’m sure they can’t be pivoted anyway. 
  • You can use a HAVING clause, or include WITH CUBE or WITH ROLLUP in the SELECT statement and it will return the appropriate summary rows for each group. 
  • You can even ORDER BY cross-tabbed columns, something you can’t do with the TRANSFORM statement in Access.

I’m spending some time working on enhancements to this procedure (for a follow-up article), to allow things like subqueries, multiple pivoting columns, non-aggregate values (e.g. – show the store with the highest sales each month, pivoted by book type). I would love to hear from anyone who has suggestions or solutions on how to improve this code.

Enjoy!

Yazı kategorisi: Cross-Tabs, Pivot Tables | » yorum bırak;