What is the 'when keyword' used for in T-SQL? - sql-server

What is the when keyword used for in T-SQL?
when
NOTE: I tried searching this on the web (e.g. 'Googling')... however due to the ubiquitous nature of the word 'when', I wasn't able to find a good explanation.
Furthermore, a list of SQL keywords did not include 'when' so either the list was not exhaustive or it is unique to T-SQL (or perhaps it was added in some 'newer' version of T-SQL / SSMS). Link to this particular SQL keyword site:
https://www.w3schools.com/sql/sql_ref_keywords.asp

It's used in conjunction with the CASE keyword, which is like a switch, or 'if' statement essentially... for example:
SELECT
CASE WHEN [Column] = 1 THEN 'Column is 1'
WHEN [Column] = 2 THEN 'Column is 2'
ELSE 'Column is not 1 or 2'
END AS [Description]

WHEN is also part of MERGE statement:
Runs insert, update, or delete operations on a target table from the results of a join with a source table
MERGE
[ INTO ] <target_table> [ WITH ( <merge_hint> ) ] [ [ AS ] table_alias ]
USING <table_source>
ON <merge_search_condition>
[ WHEN MATCHED [ AND <clause_search_condition> ]
THEN <merge_matched> ] [ ...n ]
[ WHEN NOT MATCHED [ BY TARGET ] [ AND <clause_search_condition> ]
THEN <merge_not_matched> ]
[ WHEN NOT MATCHED BY SOURCE [ AND <clause_search_condition> ]
THEN <merge_matched> ] [ ...n ]
;

Related

snowflake show tables with cluster_by

I can use show tables in <database name> to show all tables in a database.
The results returned show if a table has clustering enabled - shows the cluster_by column.
Is there a way to get back a list of all tables that have value in cluster_by ?
The documentation for show-tables shows only:
SHOW [ TERSE ] TABLES [ HISTORY ] [ LIKE '<pattern>' ]
[ IN { ACCOUNT | DATABASE [ <db_name> ] | SCHEMA [ <schema_name> ] } ]
[ STARTS WITH '<name_string>' ]
[ LIMIT <rows> [ FROM '<name_string>' ] ]
You can always ask INFORMATION_SCHEMA:
SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, CLUSTERING_KEY
FROM INFORMATION_SCHEMA.TABLES
WHERE CLUSTERING_KEY IS NOT NULL;
or using RESULT_SCAN
SHOW TABLES IN DATABASE TEST;
SELECT *
FROM TABLE(result_scan(last_query_id()))
WHERE "cluster_by" <> '';
Reference: INFORMATION SCHEMA TABLES VIEW, RESULT_SCAN

What is LEVEL equivalent in snowflake

I have to convert one oracle query to snowflake,which has a where clause LEVEL > 1. Could you please suggest me the best option.
Thanks.
I don't think it's an exact match, but the closest thing is the "start with" clause of Snowflake's connect by:
SELECT <column_list> [ , <level_expression> ]
FROM <data_source>
START WITH <predicate>
CONNECT BY [ PRIOR ] <col1_identifier> = [ PRIOR ] <col2_identifier>
[ , [ PRIOR ] <col3_identifier> = [ PRIOR ] <col4_identifier> ]
...
...
You can provide a where clause on the start with predicate, but without the "where" keyword. You can read more about it here: https://docs.snowflake.com/en/sql-reference/constructs/connect-by.html
There is level in snowflake. The differences from Oracle are:
In snowflake it's neccesary to use prior with connect by expression.
And you can't just select level - there should be any existing column in the select statement.
Example:
SELECT LEVEL, dummy FROM
(select 'X' dummy ) DUAL
CONNECT BY prior LEVEL <= 3;
LEVEL DUMMY
1 X
2 X
3 X
4 X

copy only tables with data from one database to another database

I have two database , dbOne(version - 10.50.1600 - locate in office server ) and dbTwo(version - 10.0.1600 - locate in my local server) .
I want to copy dbOne's tables with data to dbTwo .
Is there any way or script to do it ? I don't want to upgrade my local server-version !
"Import and Export Data" tool provided by SQL Server is a good tool to transfer data between two different servers.
How about generating the database scripts like in the following artcles
http://www.codeproject.com/Articles/598148/Generate-insert-statements-from
and
http://msdn.microsoft.com/en-us/library/ms186472(v=sql.105).aspx
Its possible to transfer data from one server to another server using SQL linked server query, if both are in a same network. below are the steps
Copying table structures
Generate script of all tables from server1 database then excute in server2 database. using Generate Script utility
Copying table data
sp_addlinkedserver [ #server= ] 'server' [ , [ #srvproduct= ] 'product_name' ]
[ , [ #provider= ] 'provider_name' ]
[ , [ #datasrc= ] 'data_source' ]
[ , [ #location= ] 'location' ]
[ , [ #provstr= ] 'provider_string' ]
[ , [ #catalog= ] 'catalog' ]
Insert into databaseserver2.db1.table1(columnList)
select columnList
from databaseserver1.db1.table1
Here are general steps you need to take in order for this to work
Migrating tables
Create scripts for tables in db1. Just right click the table and go to “Script table as -> Create to”
Re-order the scripts so that tables that don’t depend on any other tables are executed first
Execute scripts on db2
Migrating data
The most convenient way is to use SQL Server Import/Export wizard

Linked Server in SQL Server?

Is it possible to create database in linked server?
If yes then how.
I appreciate your help.
Thank You.
If your linked server allows it, then you can run sp_executesql remotely and by that means you can do absolutely anything on the linked server. Eg. create a database:
exec <linkedserver>.master.sys.sp_execute_sql N'create database foo';
See:
MSDN: Linking Servers
You basically need to call the sp_addlinkedserver stored proc:
sp_addlinkedserver
[ #server= ] 'server' [ , [ #srvproduct= ] 'product_name' ]
[ , [ #provider= ] 'provider_name' ]
[ , [ #datasrc= ] 'data_source' ]
[ , [ #location= ] 'location' ]
[ , [ #provstr= ] 'provider_string' ]
[ , [ #catalog= ] 'catalog' ]
Something like:
EXEC sp_addlinkedserver #server='S1_instance1', #srvproduct='',
#provider='SQLNCLI', #datasrc='S1\instance1'
For details, see the MSDN docs - it's really pretty good!
if you want create link server in sql server you can go 2 way:
1- write query.
2- use with SQL Server Management Studio with open Object Explorer and expand Server
Objects, right-click Linked Servers and then click New Linked Server.
I want to say, write query:
if you write this query you will create link server:
EXEC sp_addlinkedserver #server = [The_server_address_you_want_have_it]
EXEC sp_addlinkedsrvlogin [ #rmtsrvname = ] 'rmtsrvname'
[ , [ #useself = ] { 'TRUE' | 'FALSE' | NULL } ]
[ , [ #locallogin = ] 'locallogin' ]
[ , [ #rmtuser = ] 'rmtuser' ]
[ , [ #rmtpassword = ] 'rmtpassword' ]
for example:
EXEC sp_addlinkedserver #server = "1.1.1.1"
EXEC sp_addlinkedsrvlogin '1.1.1.1'
,'false'
,NULL
,'yes'
,'123'

Remote Computer Name in SQL Server

I have an app that run on many computers and connect to sql server
I want to log the machine names of that computers in a table every time they connect how can I do that
I want to know if there is a command like that
"Select ##MachineName"
It's up to you how you want to log this information, but HOST_NAME() returns the name of the workstation connecting to the server.
Create linked server : (allowing access to distributed, heterogeneous queries against OLE DB data sources.) using following command :
sp_addlinkedserver [ #server= ] 'server' [ , [ #srvproduct= ] 'product_name' ]
[ , [ #provider= ] 'provider_name' ]
[ , [ #datasrc= ] 'data_source' ]
[ , [ #location= ] 'location' ]
[ , [ #provstr= ] 'provider_string' ]
[ , [ #catalog= ] 'catalog' ]
Then access is like :
Select * from [server-name].[db-name].dbo.[tablename]
Also, make sure security login you are using on both the servers is same (or atleast exists on other server too).

Resources