In SQL Server, you can use the @@ROWCOUNT
system function to return the number of rows affected by the last T-SQL statement.
For example, if a query returns 4 rows, @@ROWCOUNT
will return 4.
In SQL Server, you can use the @@ROWCOUNT
system function to return the number of rows affected by the last T-SQL statement.
For example, if a query returns 4 rows, @@ROWCOUNT
will return 4.
The SQL Server BETWEEN
operator allows you to test between a range of values in your queries. For example, you could test that a value is between two numbers.
The BETWEEN
operator is inclusive, meaning that it includes the values that you specify within the range. That is, it includes values that are greater than or equal to the lower value, and values that are less than or equal to the higher value. Therefore it’s like using >=
and <=
.
In SQL Server, the ANSI_NULLS
setting allows you to specify how NULL
values are treated in queries.
You can use the SESSIONPROPERTY()
function to check the ANSI_NULLS
setting for the current session.
When you execute a stored procedure that returns a result set in SQL Server, the columns returned are defined in the stored procedure.
But did you know that you can redefine those columns?
What I mean is, you can change the names and/or the data type of the columns returned in the result set.
This could save you from having to fiddle with the column headers and data formats in the event you needed to use that result set in another setting.
For example, if a stored procedure returns a datetime2 column, but you only need the date part, you could specify date for that column, and your result set will only include the date part.
And the best part is that you can do it as part of the EXECUTE
statement. No need to massage the data after executing the procedure. way to do this is by using the WITH RESULT SETS
clause of the EXECUTE
statement.
If you encounter error Msg 11537, Level 16 in SQL Server, chances are that you’re trying to execute a stored procedure by using the WITH RESULT SETS
clause, but you haven’t included all the columns in your definition.
When you use the WITH RESULT SETS
clause in the EXECUTE
/EXEC
statement, you must provide a definition for all columns returned by the stored procedure. If you don’t, you’ll get this error.
Have you ever run a stored procedure, only to be overwhelmed at the number of columns returned? Maybe you only needed one or two columns, but it presented you with way too many columns for your needs on this particular occasion.
Fortunately, there’s a little trick you can use to retrieve selected columns from a stored procedure. This enables you to get just the columns you need.
And the best part is, it doesn’t involve having to create temporary tables and shuffling the data around.
All you need to do is pass your stored procedure to the OPENROWSET()
function.
The same concept can be applied to the OPENQUERY()
function.
If you’re getting a “profile name is not valid” error when updating a Database Mail profile in SQL Server, it could be that you’ve forgotten to provide the profile ID.
When you update a Database Mail profile with the sysmail_update_profile_sp
stored procedure, you need to include the profile ID if you want to update the profile name.
RPC stands for Remote Procedure Calls. It needs to be enabled before you can execute stored procedures on a linked server.
If you’re not sure whether it’s enabled on a linked server, you can check it’s setting by querying the sys.servers
system catalog view.
One way of creating a database on a linked server, is to simply jump over to that server and create it locally.
But you’d probably feel a bit cheated if I included that as one of the “2 ways” to create a database on a linked server.
Also, while that option is fine if you’re able and willing to do it, this article shows you how to do it remotely using T-SQL, without having to jump over to the local server. Plus you might find this technique quicker than jumping over to the other server.
Both of the “2 ways” involve the EXECUTE
statement (which can also be shortened to EXEC
). We can use this statement to execute code on the linked server, and that includes creating a database on it.
If you need to create a table on a linked server, you can do this either directly on the remote server, or you can do it by running a script from your local server.
Here are two ways of using T-SQL to create a table on a linked server.
Both methods use the EXECUTE
/EXEC
statement. But each uses a different approach.