Monday, 24 October 2016

SQL - While Loop Syntax

Below is the WHILE LOOP Syntax in SQL

DECLARE @cnt INT = 1;

WHILE @cnt BETWEEN 1 AND 20
BEGIN

PRINT @cnt;
SET @cnt = @cnt+1;

END

SQL Doesn't have For Loop, instead you have to use While loop.

SQL - For Loop Syntax

SQL doesn't have For Loop, instead you have to use While loop.
The syntax is as below.


DECLARE @cnt INT = 1;

WHILE @cnt BETWEEN 1 AND 20
BEGIN

PRINT @cnt;
SET @cnt = @cnt+1;

END


Thursday, 18 August 2016

SQL Server - Get DB Object Names, Count

SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'

The above example lists all the tables in the database.
You can modify the query to access other details about the db objects.
Also the same can be used to count the number of objects (tables / stored procedures) in the database.

Below are the reference to the other XTYPE values corresponding to each objects.

-- AF: Aggregate function (CLR)
-- C: CHECK constraint
-- D: Default or DEFAULT constraint
-- F: FOREIGN KEY constraint
-- L: Log
-- FN: Scalar function
-- FS: Assembly (CLR) scalar-function
-- FT: Assembly (CLR) table-valued function
-- IF: In-lined table-function
-- IT: Internal table
-- P: Stored procedure
-- PC: Assembly (CLR) stored-procedure
-- PK: PRIMARY KEY constraint (type is K)
-- RF: Replication filter stored procedure
-- S: System table
-- SN: Synonym
-- SQ: Service queue
-- TA: Assembly (CLR) DML trigger
-- TF: Table function
-- TR: SQL DML Trigger
-- TT: Table type
-- U: User table
-- UQ: UNIQUE constraint (type is K)
-- V: View
-- X: Extended stored procedure

Thursday, 21 July 2016

Windows 2012 - Install IIS on Windows Server 2012 R2

Installing and configuring IIS on Windows 2012 R2 is well explained in the below article with step by step screenshots.
Below is the link to the article:

Wednesday, 20 July 2016

XSL - If Else condition Choose When Otherwise

In XSL, there is no If - Else construct. Only If is present.

If your requirement is stricly If - Else, instead use the Choose When Otherwise construct as below:

<xsl:choose>
 <xsl:when test="$a > $b">
  <h2> A is greater than B </h2>
 </xsl:when>
 <xsl:otherwise>
  <h2> B is greater than A </h2>
 </xsl:otherwise>
</xsl:choose>

In the above example, a ($a) and b ($b), are variables.

Tuesday, 19 July 2016

XSL - Check if item is contained in a list

Use the below sample if you want to check if an item is contained in a list.
This sample is using XSL and XPATH with XML input.
In the below sample:
<!-- .xsl file -->

<!-- List of items to be checked against -->
<xsl:variable name="students" select="'Jon Tom Ana Ben'" />

<!-- Item to be checked if contained in list -->
<xsl:variable name="classTopper" select="'Ana'" />

<!-- Alternately the item can be dynamically taken from the xml input file as below -->
<!-- <xsl:variable name="classTopper" select="/root/@topper" /> -->

<!-- Check if class topper is in the students list -->
<xsl:if test="
  contains(
    concat(' ', $students, ' '),
    concat(' ', $classTopper, ' ')
  )
">
  <xsl:value-of select="concat('Student ', $classTopper, ' is in the students list.')" />
</xsl:if>

Note: Here space is used as separator in the list. You can replace with something else like a pipe "|". In that case, the concat in contains() will be like:
concat ('|', $list, '|') ...

Thursday, 14 July 2016

JavaScript - Replace Comma with New Line

Use JavaScript String functions "split" and "join":
var formattedString = yourString.split(",").join("\n")
If you'd like the newlines to be HTML line breaks replace "\n" with "<br />"

Alternate method using "replace" function.
yourString.replace(/,/g, '\n');