|
|
|
|
|
|
Question of the Day |
Today's question (by Greg Larsen): | |
Inlining Scalar UDFs | |
Scalar user defined functions (UDFs) can cause performance issues that can often be difficult to troubleshoot. Microsoft introduced a new 2019 feature called T-SQL Scalar UDF Inlining that can improve performance of some of these functions without changing any of your code. Which of the functions can take advantage of this feature? (choose 3)
--Function 1 CREATE OR ALTER FUNCTION dbo.GetRating_1(@CustomerID INT) RETURNS VARCHAR(20) AS BEGIN DECLARE @Rating VARCHAR(20); SELECT @Rating = Rating FROM Customer WHERE CustomerID = @CustomerID; RETURN @Rating; END GO CREATE OR ALTER FUNCTION dbo.GetRating_2(@CustomerID INT) RETURNS VARCHAR(20) AS BEGIN DECLARE @Rating varchar(20); IF @CustomerID % 4 = 0 BEGIN SET @Rating = 'Diamond'; END ELSE IF @CustomerID % 4 = 1 BEGIN SET @Rating = 'Gold'; END ELSE IF @CustomerID % 4 = 2 BEGIN SET @Rating = 'Silver'; END ELSE IF @CustomerID % 4 = 3 BEGIN SET @Rating = 'Bronze'; END RETURN @Rating; END GO --Function 3 CREATE OR ALTER FUNCTION dbo.GetRating_3(@CustomerID INT) RETURNS VARCHAR(20) AS BEGIN DECLARE @Rating varchar(20); DECLARE @RatingID INT; SELECT @RatingID = RatingID FROM Customer WHERE CustomerID = @CustomerID; IF @RatingID = 0 BEGIN SET @Rating = 'Diamond'; END ELSE IF @RatingID = 1 BEGIN SET @Rating = 'Gold'; END ELSE IF @RatingID = 2 BEGIN SET @Rating = 'Silver'; END ELSE IF @RatingID = 3 BEGIN SET @Rating = 'Bronze'; END RETURN @Rating; END GO --Function 4 CREATE OR ALTER FUNCTION dbo.GetRating_4(@CustomerID INT) RETURNS VARCHAR(20) AS BEGIN DECLARE @Ratings TABLE(RatingID INT, RatingDesc varchar(20)); DECLARE @Rating VARCHAR(20); INSERT INTO @Ratings(RatingID, RatingDesc) SELECT RatingID, RatingDesc FROM Rating; SELECT @Rating = R.RatingDesc FROM Customer AS C JOIN @Ratings AS R ON C.RatingID = R.RatingID WHERE CustomerID = @CustomerID; RETURN @Rating; END GO --Function 5 CREATE OR ALTER FUNCTION dbo.GetRating_5(@RatingID INT) RETURNS VARCHAR(20) AS BEGIN DECLARE @ID INT = 0; DECLARE @Rating varchar(20); WHILE @ID <> @RatingID BEGIN SET @ID = @ID + 1; END; SELECT @Rating = RatingDesc FROM Rating WHERE RatingID = @ID; RETURN @Rating; END | |
Think you know the answer? Click here, and find out if you are right. |
Yesterday's Question of the Day (by Steve Jones - SSC Editor) |
A chart of costs in R I have this dataframe in R: > head(sales) Date Day Month Year Customer_Age Age_Group Customer_Gender 1 2013-11-26 26 November 2013 19 Youth (<25) M 2 2015-11-26 26 November 2015 19 Youth (<25) M 3 2014-03-23 23 March 2014 49 Adults (35-64) M 4 2016-03-23 23 March 2016 49 Adults (35-64) M 5 2014-05-15 15 May 2014 47 Adults (35-64) F 6 2016-05-15 15 May 2016 47 Adults (35-64) F I am looking to get this histogram of unit costs: What should I run to get this? Answer: hist(sales$Unit_Cost) Explanation: A quick histogram is from the hist() function. Ref: hist() - https://www.datamentor.io/r-programming/histogram/ |
Database Pros Who Need Your Help |
Here's a few of the new posts today on the forums. To see more, visit the forums. |
SQL Server 2017 - Administration |
Old, unnecessary SQL Server windows updates keep failing - any ideas? - Background My organization tightly controls what’s on our internal windows update server. The version of SQL Server that I have installed is Microsoft SQL Server 2017 (RTM-CU20) (KB4541283) - 14.0.3294.2 (X64) Mar 13 2020 14:53:45 Copyright (C) 2017 Microsoft Corporation Developer Edition (64-bit) on Windows 10 Enterprise 10.0 (Build 17134: ) (Hypervisor) For some […] |
One of six 2017 instances offline - Always On. - Did patch Tuesday yesterday across 6 UCS blades. Servers are 2019 datacenter in a cluster. Each blade has 4 instances so 4 availability groups of 6 members each. One of the 2017 instances is in a bad state. I've tried a repair, I've tried removing updates (even though the other 5 prod blades are fine, […] |
SQL 2017 AGL - only accessible locally - Hi, I've got 2 x SQL 2017 CU20 (Dev edition) boxes in a Windows cluster with an AG and listener (non readable sync secondary). The DBs are synchronised and the AGL is contactable through SSMS on the primary server when I'm RDPd on. The SPN is set up for the AGL name with the SQL […] |
SQL Server 2017 - Development |
Bulk Insert & link relationship between inserted row - Hi, Let me elaborate with example:- I having an existing TableA with below sample column & data Date Amount Category Name 2018-01-01 […] |
SQL Server 2016 - Development and T-SQL |
CTE left join not giving me correct result - Hello, Why is the Left Join with a CTE giving me wrong results? I want to know which Emails are not in Table B using their UserIDs Instead it's giving me all the records from CTE and Null values from the Table B. What am I doing wrong? Many thanks. ; With CTE as ( […] |
Encryption table and saving in 2016 - Hi, How would you encrypt data from multiple tables and save in a temp tables using a script (tsql)? please share general logic in sql 2016 version. Should we use always on encryption? or something else and then also how do you decrypt the data from the same temp table and save those in the […] |
SQL Server 2019 - Administration |
Queries run by a particular login or Database user in SQL Server 2016 - Hello, Is there a way in SQL Server to find out which queries were run by particular login in SQL Server and on which databases? If somebody can provide a script, that will be great. Thanks in advance |
Trouble installing latest version of SSMS - Last night I went to the control panel and removed any programs that had 'SQL' in its name. That was the 2017 version of SSMS. I wanted to start fresh. I downloaded SSMS 18. I'm not sure of the server name, so I tried to find it here: https://docs.microsoft.com/en-us/sql/ssms/tutorials/ssms-tricks?view=sql-server-ver15#find-the-error-log-location-if-you-cant-connect-to-sql-server under the section titled "Find the […] |
SQL Server 2019 - Development |
Function issue with union (two databases) - I want this function to retrieve the latest buyer from two databases db1 and db2. It worked for a single db. Now I try to use UNION to query both dbs an get the latest of the two It won't accept this function "Select statements included within a function cannot return data to a […] |
Export sql results to csv - Hi all, Hopefully you can help me. I have created a sql job which returns some results and then exports it to a csv file. But each column is merged into one cell. The way I have done this is using the sqlcmd feature as per the below: sqlcmd -i Is there […] |
How to use stored procedure result set into a script task in SSIS - Hello, guys! I created a SSIS package in Visual Studio Community 2019: Everything is working fine, but I don't know how to pass the data from "Commutations" (SQL Task) to "Net Present Value" (Script Task). Basically, "Commutations" generates a result set that I'm passing to a SSIS object variable. This result set has a couple […] |
Pivot Table Problem - I am having a huge brain fart on how to accomplish what I want. I want to pivot the attached set of data (sql file) to look like this (I know its an image but I am not savy enough to plop an actual result set in here). The attached file creates a temp file […] |
Integration Services |
Execute Powershell Script from Script Task - Has anyone managed to execute a PoSh script from within a Script Task? When I started looking at this, it seemed like it would be straightforward enough and the process is described here. But ... the DLL which that article refers to (and which appears to be necessary to execute PoSh from C#) is called […] |
COVID-19 Pandemic |
Daily Coping 21 May 2020 - Today’s tip is to reflect on what makes you feel really valued and appreciated. My thoughts: http://voiceofthedba.com/2020/05/22/daily-coping-22-may-2020/ |
Daily Coping 20 May 2020 - Today’s tip is: Hand-write a note to someone you love and send them a photo of it. http://voiceofthedba.com/2020/05/20/daily-coping-20-may-2020/ |
| |
©2019 Redgate Software Ltd, Newnham House, Cambridge Business Park, Cambridge, CB4 0WZ, United Kingdom. All rights reserved. webmaster@sqlservercentral.com |