Select multiple columns from a subquery in Select Statement with SQL Server Or Use OF Outer Apply
- SELECT t1.ID, t1.fck, t1.f1, t3.f2, t3.f3
- FROM tbl1 AS t1
- OUTER APPLY (
- SELECT f2, f3
- FROM tbl2 AS t2
- WHERE t2.ID = t1.fck) AS t3
Decaaring table as Variable And Inserting In it:
- declare @listOfTablesArrangementIDs table (id int);
- insert @listOfTablesArrangementIDs(id) values(1),(2),(3)
- insert @listOfTablesArrangementIDs(id) values(2027),(20258),(7)
- select * from TablesArrangement where ID NOT IN (select * from @listOfTablesArrangementIDs ) AND AreaCode = 'tst1'
Create Trigger
- -- =============================================
- -- Author: <Muhammad Ashikuzzaman,,Name>
- -- Create date: <24-10-2018,,>
- -- Edit date: <14-08-2018, >
- -- Description: <Initial server: 192.168.153.208 >
- -- =============================================
- CREATE TRIGGER [ccl2].[TRG_TBL_PriceSetup]
- ON [ccl2].[TBL_PriceSetup]
- AFTER UPDATE
- AS
- BEGIN
- -- SET NOCOUNT ON added to prevent extra result sets from
- -- interfering with SELECT statements.
- SET NOCOUNT ON;
- --Declare @PriceID bigint
- --Select @PriceID = PriceID From deleted
- insert into ccl2.ARC_PriceSetup (PriceID,Price)
- select PriceID,Price from deleted
- END
Create A Stored Procedure
- USE [ProjectInfoDB]
- GO
- /****** Object: StoredProcedure [dbo].[SPROC_GET_tblDyesForecastingHead_LDNO] Script Date: 11/1/2018 12:44:51 PM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- -- =============================================
- -- Author: <Muhammad Ashikuzzaman,,Name>
- -- Create date: <14-08-2018,,>
- -- Edit date: < ,,>
- -- Description: <Initial server: 192.168.153.208 ,,>
- -- =============================================
- ALTER PROCEDURE [dbo].[SPROC_GET_tblDyesForecastingHead_LDNO]
- @LDNO bigint = null,
- @Result bit =1 out ,
- @Message nvarchar(200) = 'ok' out
- AS
- BEGIN
- SET NOCOUNT ON;
- --select top 50 CommonId_For_ForeCastingDetails, DyeingPart, DyesID from dbo.tblDyesForecastingDetail
- select DISTINCT LDNO from dbo.tblDyesForecastingHead
- where Convert(nvarchar(50), LDNO) like '%'+ Convert(nvarchar(50), ISNULL(@LDNO, LDNO) ) +'%'
- --select top 50 CommonId_For_ForeCastingDetails, DyeingPart, DyesID from dbo.tblDyesForecastingDetail
- If (@@ERROR<>0)
- Begin
- set @Result = 0
- set @Message = 'Failed to Retrieve, Try Again'
- End
- Else
- BEGIN
- set @Result = 1
- set @Message = 'Data was Retrieved successfully'
- END
- END
Create a Cursor and traverse selected row through this cursor
- declare @Test table (Id int, Col1 int, Col2 int, Col3 int)
- insert into @Test (Id , Col1 , Col2 , Col3 )
- values (1 , 1 , 1 , 40 ),
- (2 , 1 , 1 , 41 ),
- (3 , 1 , 1 , 41 ),
- (4 , 1 , 1 , 42 ),
- (5 , 1 , 2 , 40 ),
- (6 , 1 , 3 , 41 )
- declare @Id bigint
- declare @col1 int
- declare @col2 int
- declare @col3 int
- declare @CtnInfo cursor
- set @CtnInfo = CURSOR FOR
- SELECT Id, COL1, COL2, COL3 FROM @Test
- declare @tblBody nvarchar(max)
- set @tblBody= 'Start : '
- OPEN @CtnInfo
- SELECT @@FETCH_STATUS as FETCH_STATUS1
- WHILE 1 = 1
- BEGIN
- FETCH NEXT FROM @CtnInfo INTO
- @id, @col1, @col2, @col3
- IF @@FETCH_STATUS = -1
- Break
- Else
- SET @tblBody=@tblBody+ ', ' + convert(nvarchar(20), @Id)
- END
- CLOSE @CtnInfo
- DEALLOCATE @CtnInfo
- SELECT @@FETCH_STATUS as FETCH_STATUS2
- select @tblBody as TotalSelecteddata
Understanding partition by
- -- My understanding partition by will count seqs increasingly until partition by column value remain unchanged
- declare @Test table (id int, Col1 int, Col2 int, Col3 int)
- insert into @Test (id , Col1 , Col2 , Col3 )
- values (1 , 1 , 1 , 40 ),
- (1 , 1 , 1 , 41 ),
- (1 , 1 , 1 , 41 ),
- (1 , 1 , 1 , 42 ),
- (1 , 1 , 2 , 40 ),
- (1 , 1 , 3 , 41 ),
- (1 , 1 , 3 , 41 ),
- (2 , 1 , 4 , 43 ),
- (2 , 2 , 1 , 43 ),
- (2 , 2 , 1 , 44 ),
- (2 , 2 , 2 , 44 ),
- (2 , 2 , 3 , 44 ),
- (2 , 2 , 4 , 45 ),
- (3 , 3 , 1 , 46 ),
- (4 , 3 , 2 , 46 ),
- (5 , 3 , 2 , 47 )
- -- Normal Group BY
- select col1, col2 , count(Col1) Col1Count, count(Col2) Col2Count
- from @Test
- group by col1, col2 order by Col1
- --partition BY With Group BY
- -- My understanding partition by will count seqs increasingly untill partition by column value remain unchanged
- select col1, col2, col3, count(col3)Col3Count, row_number()
- over (Partition BY col1 , col2
- order By col1 ) seqs
- from @Test
- group by col1, col2, col3
- order by col1, col2
Remove Time portion from a date time
select DATEADD(DD, DATEDIFF(dd, 0, '2018-12-25 12:25:00' ), 0)-- will remove time portion from the date
select DATEDIFF(dd, 0, '2018-12-25 12:25:00' ) here 2nd parameter 0 means '1900-01-01'
select DATEADD(DD, DATEDIFF(dd, 0, '2018-12-25 12:25:00' ), 0)-- will remove time portion from the date
select DATEDIFF(dd, 0, '2018-12-25 12:25:00' ) here 2nd parameter 0 means '1900-01-01'
Send Mail By sp_send_dbmail stored procedure
- declare @tblBody nvarchar(max)
- set @tblBody='<style type="text/css">
- #detail_table {
- border: 2px solid gray;
- border-collapse: collapse;
- }
- #one td { border: 1px solid #00000; }
- #detail_table td , #detail_table th {
- text-align: center;
- border: 1px solid gray;
- } </style>'
- set @tblBody=@tblBody+'<div align="center" style=" border:0px solid #000000; width: 98%;height: 95%;padding: 8px;padding-top:0px; margin: 5px;"> <div align="center" style="font-weight: bold;font-size: 22px;"> HamdoonSoft Complex </div> <div style="clear:both;padding: 7px;"></div> <div align="center" style="font-weight: bold;font-size: 16px; width:230px; border-bottom: 1px solid #222;position:absolute; left:42%"> Hamdoon Soft Notification </div> <div style="clear:both;"></div>
- <div align="left" style="font-size:12px;border:0px solid #000000; width: 99%;height: 100%;padding: 20px;padding-top:0px;">
- <div align="left" style="margin-top:35px;"><b> Date: </b> '+ convert( nvarchar(30) , GETDATE() )+'</div>
- <div align="left" style="margin-top:8px;">To, </div>
- <div align="left" style="margin-top:10px; margin-left:20px;">DGM/ AGM/ Sr. Manager</div> <div align="left" style="margin-top:5px; margin-left:20px; height: 30px; "> Department: '+ '@ResponsibleDepartment' +'</div> <div align="left" style="margin-top:5px; margin-left:22px;">Subject: Problematic fabric information from off line Inspection to problamatic department.</div> <div align="left" style="margin-top:18px;">Dear Sir</div> <div align="left" style="margin-top:15px; margin-left:20px;">This is to inform you that, we found below batch problematic during 100% inspection in off line quality zone.</div>
- <div align="left" style="margin-top:8px;">Details :- </div>
- <br/> <table id="detail_table" style="margin-top:20px; margin-right:.5%; width:99.5%; " > <tr id="one"> <th>C1 </th> <th>C2 </th> <th>C3 </th> <th>C4 </th> <th>C5</th> </tr> '
- DECLARE @c1 NVARCHAR(40)
- DECLARE @c2 NVARCHAR(40)
- DECLARE @c3 NVARCHAR(40)
- DECLARE @c4 NVARCHAR(40)
- DECLARE @c5 NVARCHAR(40)
- declare @CtnInfo cursor
- set @CtnInfo = CURSOR FOR
- SELECT * from (values (1, 2, 3, 4, 5) , (1, 2, 3, 4, 5) )
- t21 (c1, c2, c3, c4, c5)
- OPEN @CtnInfo
- WHILE 1 = 1
- BEGIN
- FETCH NEXT FROM @CtnInfo INTO
- @c1, @c2, @c3, @c4, @c5
- IF @@FETCH_STATUS = -1
- Break
- Else
- set @tblBody=@tblBody+' <tr >
- <td align="center" > '+@c1+' </td> <td align="center" > '+@c2 +' </td> <td align="center" > '+@c3 +' </td> <td align="center" > '+@c4+' </td><td align="center" > '+@c5+'</td> </tr> '
- END
- CLOSE @CtnInfo
- DEALLOCATE @CtnInfo
- SELECT @@FETCH_STATUS as FETCH_STATUS2
- select @tblBody as TotalSelecteddata
- set @tblBody=@tblBody+' </table> <br/> <div style="position: relative!important;left: -530px!important; top: 10px!important; font-weight:bold; font-family:Verdana, Arial, Helvetica, sans-serif;"> </div> </div> </div>'
- Set @tblBody=@tblBody+'<div style="Font-weight:bold; margin-top:30px;font-family:Verdana, Arial, Helvetica, sans-serif; "> Best Regards </div> <div style=" Font-weight:bold; margin-top:10px;font-family:Verdana, Arial, Helvetica, sans-serif; "> MIS Department </div>'
- declare @recipient nvarchar(max) = 'ashikcse20@gmail.com;'
- declare @copy_recipient nvarchar(max) = ' ashikcse20@gmail.com;'
- EXEC msdb.dbo.sp_send_dbmail
- @profile_name = 'MIS-MTL',
- @recipients = @recipient,
- @copy_recipients= @copy_recipient ,
- @body = @tblBody ,
- @subject = 'Automatic Mail Sending',
- @body_format = 'HTML'
No comments:
Post a Comment