/* Cross Join: Each row in the first table is paired with all the rows in the second table. */ if OBJECT_ID('tempdb..#a') is not null drop table #a if OBJECT_ID('tempdb..#b') is not null drop table #b create table #a ( acol varchar(10) ) insert into #a values ('a1'), ('a2') create table #b ( bcol varchar(10) ) insert into #b values ('b1'), ('b2') select * from #a select * from #b select * from #a cross join #b
Archive for the ‘Explanations’ Category
Posted in Computer, Database, Explanations, How To, TSQL, tagged alternative, cartesian, CROSS, explanation, how to, howto, join, Product, sql, TSQL, Usama, way on December 13, 2012| Leave a Comment »
Command like Sql can be used to run sql scripts saved in disks. Therefroe if there is a bunch of scripts
(files) they can all be added with ttheir path and file name in notepad and have it saved with anyname with a
cmd extension. Then running this file would execute all the lines in the file.
Use “sqlcmd” instead of osql because “osql” cannot handle alot of special characters like the “£” sign.
Here is an example:
sqlcmd -S MyServerName -E -d My_dbName -i “C:\MyTSQL\MyScript.sql”
The above if run in commandline will execute the script in the path.
This is usefull when we need to run batches of scripts.
To make scripts upload/commit to a server by using the above command make sure the SP/Function or other
objects are alwasy dropped and recreated e.i. starts with the create statement as below:
--For SP: --------- IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mySP]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[mySP]] GO CREATE PROCEDURE [dbo].[mySP] @var AS VARCHAR(100) as begin ... end --For Function: -------------- IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[myUDF]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) DROP FUNCTION [dbo].[myUDF] GO create FUNCTION [dbo].[myUDF] ( @var AS VARCHAR(100) ) RETURNS INT AS BEGIN ... end
Posted in Computer, Database, Explanations, How To, TSQL, tagged alternative, Binary, Bitwise, example, explanation, how to, howto, mask, Masking, sample, sql, TSQL, Usama, way on December 12, 2012| Leave a Comment »
Assume the below scenario:
A system checks a stock level for a grocery store that sells 3 items and whenever a certain item runs out it flags up a message.
Say the 3 items are:
To make use of binary masking (also known as bitwise) to be able to track which needs to be highlighted, we need to do the following:
1) We will assign a binary number to each one of the item, 1 for denoting we need to flag a message and 0 f0r doing nothing. Each of the the items in turn will have a fixed position. So in this instance there are three position for three items and we can assign it as below:
Item Position Binary Value
—– ——— ————-
Mango 0 1
Orange 1 0
Apple 2 1
2) Now, we need a single decimal number that represents which items jave a binary 1 to flag a message and which don’t. To do that we use the Position value and treat that as the power of 2.
So, we use Position 1 and 3 (because those have the binary value 1 binded) and it should equate to:
(2^0) = 1
+ (2^2) = 4
Total = 5
3) The value 5 can then be sent to the section of the system which flags messages. It should then have a script to reverse it back to a binary number which is 101.
The position number is fixed and therefore can be easily be deduced as to which binary number is binded to whcih item.
This way by only sending an integer number we can for a group of item attached and interchagne flag data.
>Returns the binary representation of a number:
-- courtesy to a script somewhere in the internet CREATE FUNCTION [csrt].[udf_ReturnBinaryPattern] ( @Byte INT ) RETURNS CHAR(8) AS BEGIN DECLARE @Pattern CHAR(8) SET @Pattern = '' SELECT @Pattern = convert(VARCHAR,+(@Byte & 1) / 1) + convert(VARCHAR,(@Byte & 2) / 2) + convert(VARCHAR,(@Byte & 4) / 4) + convert(VARCHAR,(@Byte & 8) / 8) + convert(VARCHAR,(@Byte & 16) / 16) + convert(VARCHAR,(@Byte & 32) / 32) + convert(VARCHAR,(@Byte & 64) / 64) + convert(VARCHAR,(@Byte & 128) / 128) RETURN (@Pattern) END GO
>Optional – Explaning & bitwise AND operator:
The binary representation of 170 (a_int_value or A) is 0000 0000 1010 1010. The binary representation of 75 (b_int_value or B) is 0000 0000 0100 1011. Performing the bitwise AND operation on these two values produces the binary result 0000 0000 0000 1010, which is decimal 10.
SELECT 170 & 75
Result is 10 (thisis a decimal number)
The & operator tranforms number or charters into binary and then adds them up:
170 = 0000 0000 1010 1010
75= 0000 0000 0100 1011
10 = 0000 0000 0000 1010
Another artice: http://sqlfool.com/2009/02/bitwise-operations/
--Proof of nulls and selects ---------------------------------- declare @a table (col int) insert into @a select 1 union select null union select 3 select count(*) from @a -- counts nulls select count('') from @a -- counts nulls select count(1) from @a -- counts nulls select count(col) SpecificColumn from @a -- only counts NON null --Proof of Sum and Null ----------------------- create table #a ( b int ) insert into #a values (1) insert into #a values (null) insert into #a values (1) select sum(b) from #a
Posted in Computer, Database, Errors&Resolutions, Explanations, How To, SSMS, TSQL, Uncategorized, tagged Catch, Droping, error, Global, handle Try, Handling, objects, sql, table, TSQL, Usama on September 7, 2012| Leave a Comment »
BEGIN TRY ---------------- EOF ------------------ DROP TABLE AnyTable ----------------------------------------- END TRY BEGIN CATCH PRINT 'Error Handled: ' + ERROR_MESSAGE() END CATCH IF OBJECT_ID('tempdb..##AnyGlobalTable') IS NOT NULL DROP TABLE ##AnyGlobalTable
/* My explation
Think of partition as the number of groups you want the ranking for.
Therefore it will assign a row number starting from 1 to the columns of which the same values appear in the columns specified in the partition section
So if you just have one column there it will count each and every row
If you have two columns it will start the count of rows from 1 for each time the columns values are different.
In other workds: if you have two columns it will count the ranks for two groups.
Let me explain:
Say you have a table with two fields.
In there you store names of fruits in one column and the colour in another:
If you just use any one of the column to partintion it will give your the rank from 1 -6 (total number of rows = 6).
If you put both the names of the columnns then you have rank for both the groups from (1-3)
declare @a table ( n varchar(50) , c varchar(50) ) INSERT INTO @a SELECT 'Mango', 'Orange' UNION ALL SELECT 'Mango', 'Green' UNION ALL SELECT 'Mango', 'Yellow' UNION ALL SELECT 'Melon', 'Orange' UNION ALL SELECT 'Melon', 'Green' UNION ALL SELECT 'Melon', 'Yellow' SELECT * FROM @a order by 1, 2 select * ,row_number() OVER (PARTITION BY n ORDER BY n) NameOccurance ,row_number() OVER (PARTITION BY c ORDER BY c) ColourOccurance ,row_number() OVER (PARTITION BY n, c ORDER BY n) BothOccurance from @a a order by 1, 3 -- thik of partition as group -- in plain english for every mango get the colour ranking ordered by the name mango