Donate

Show Asterisk Triangle Shapes Using TSQL In SQL Server

In programming, it's a common exercise to show triangle shapes using the asterisk character. Well, this can be achieved using TSQL as well.

Script #1
DECLARE      @Total1      INT            
 DECLARE      @ctrOuter1      INT            
 DECLARE      @ctrInner1      INT            
 DECLARE @strPrint1 VARCHAR(100)  
 SET           @Total1     = 5  
 SET           @ctrOuter1 = @Total1  
 SET           @strPrint1 = ''  
 WHILE      @ctrOuter1     >= 1 --outer loop  
 BEGIN   
      SET @strPrint1 = ''  
      SET @ctrInner1 = 0  
      WHILE     @ctrInner1 <= (@Total1 - @ctrOuter1)   
      BEGIN  
           SET @strPrint1 = @strPrint1 + '*'  
           SET @ctrInner1 = @ctrInner1 + 1  
      END       
   PRINT @strPrint1       
      SET @strPrint1 = @strPrint1 + CHAR(13)  
      SET     @ctrOuter1 = @ctrOuter1 - 1     
 END  
Output:
Show Asterisk Triangle Shapes Using TSQL In SQL Server
Script #2
DECLARE      @Total      INT            
 DECLARE      @ctrOuter      INT            
 DECLARE      @ctrInner      INT            
 DECLARE    @strPrint VARCHAR(100)  
 SET           @Total     = 5  
 SET           @ctrOuter = @Total  
 SET           @strPrint = ''  
 WHILE      @ctrOuter     >= 1 --outer loop  
 BEGIN   
      SET @strPrint = ''  
      SET @ctrInner = 1  
      WHILE     @ctrInner <= @ctrOuter  
      BEGIN  
           SET @strPrint = @strPrint + '*'            
           SET @ctrInner = @ctrInner + 1  
      END       
   PRINT @strPrint       
      SET @strPrint = @strPrint + CHAR(13)  
      SET     @ctrOuter = @ctrOuter - 1     
 END  
Output:
Show Asterisk Triangle Shapes Using TSQL In SQL Server Cheers! :)

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.