Posts

Showing posts from December, 2019

sp_executesql – Let's learn How to Use It

Image
In the post SQL Server - Using SQL Agent JOBs to Scheduleand Execute Multiple Stored Procedures in Parallel , I’ve demonstrated the usage of sp_executesql and how powerful and helpful it is. This time I’ll put more explanations about it. Talk is cheap, and let’s see the code. DECLARE @ModDate datetime = '2017-12-04 00:00:00.000' SELECT        [AddressID]     , [AddressLine1]     , [AddressLine2]     , [City]     , [StateProvinceID]     , [PostalCode]     , [SpatialLocation]     , [rowguid]     , [ModifiedDate] FROM [AdventureWorks2017] . [Person] . [Address] WHERE [ModifiedDate] <= @ModDate The above code is straightforward enough. Now let’s consider the below scenarios – suppose our database name is composed by the keyword ‘AdventureWorks’ plus the year number suffix (in our example it’s 2017); ...

Using Multiple EXISTS Statement to Make Judgement Against Scenarios - Be Careful

Image
The EXISTS statement is beloved by many SQL specialists and developers. After all, it's simple in the syntax, easy to use and quite straightforward - so straightforward that we hardly give it a thought about how it really works before we use it. That type of ignorance and arrogance got me caught in one of my previous work. I hope by sharing my experience here you're not making the same mistake I once made. Talk is cheap so let's see the code. declare @test TABLE        (               CompanyID int               , ShareHolding int               , ShareHolderRank int        ) insert into @test values ( 1 , 5 , 2 )        ,( 1 , 3 , 4 ) select * from @test select * f...