Sql Thoughts- use of loops



In the loop, code is executed repeatedly in an iterative fashion until some condition is met that allows the code to break out of the loop.Instead of curly braces we have begin and End.
Given below is an example that shows how to use a while loop in sql.Its similar to the loop that we use in front end.

Code Snippet

DECLARE @STRT INT ,@END INT
SET @STRT =1
SET @END =10
 WHILE(@STRT <= @END) 
     
  				BEGIN 
  			  
INSERT INTO TableName VALUES(@STRT )
SET @STRT = @STRT + 1
END


if we need the iterated value to be added to a string.we can use CAST or CONVERT function.



Code Snippet


CAST (@STRT AS CHAR  )
or else 
CONVERT(CHAR, @STRT)


The link which is given below gives the whole information about Cast and convert function

Cast and convert


HappyCoding ! 😉

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: