Call SQL Sever system stored procedures with query language? - sql

I am new to SQL SERVER. And I am wondering can the system stored procedures (e.g., sp_help) can be called with query language such as select and where?

you can store the results in a table and then filter out the records:
e.g.
DECLARE #Table TABLE(
SPID INT,
Status VARCHAR(MAX),
LOGIN VARCHAR(MAX),
HostName VARCHAR(MAX),
BlkBy VARCHAR(MAX),
DBName VARCHAR(MAX),
Command VARCHAR(MAX),
CPUTime INT,
DiskIO INT,
LastBatch VARCHAR(MAX),
ProgramName VARCHAR(MAX),
SPID_1 INT,
REQUESTID INT
)
INSERT INTO #Table EXEC sp_who2
SELECT *
FROM #Table
Reference
Sp_help returns more than one result sets. The following link might be helpful in that case:
Store multiple result sets
Retrieve a single result set

Related

Cannot return a user table type form a function

I just wrote this coe example to return a user type from a function:
CREATE TYPE dbo.ScheduledActivity_TVP AS TABLE
(
Id uniqueidentifier NOT NULL primary key,
AdditionalDataTypeSignature nvarchar(100) not null,
AdditionalDataId uniqueidentifier NOT NULL,
AdmissionId uniqueidentifier NOT NULL
)
GO
CREATE OR ALTER function [dbo].[Fun_GetFollowUpBymonth](#admissionId uniqueidentifier)
returns ScheduledActivity_TVP as
begin
declare #q ScheduledActivity_TVP
insert into #q
select Id,
AdditionalDataTypeSignature,
AdditionalDataId,
AdmissionId
from ScheduledActivities
where #admissionId = ScheduledActivities.AdmissionId;
return #q
GO
And Sql Server tells me that I must declare the scalar variable #q.
What is wrong in the code above?
I don't see why you are using a multi-line table value function here; they are notoriously slow.
Use an inline table value function, which doesn't even need a TYPE:
CREATE OR ALTER FUNCTION [dbo].[Fun_GetFollowUpBymonth] (#admissionId uniqueidentifier)
RETURNS table
AS RETURN
SELECT Id,
AdditionalDataTypeSignature,
AdditionalDataId,
AdmissionId
FROM dbo.ScheduledActivities
WHERE #admissionId = ScheduledActivities.AdmissionId;

How can I pass tables to stored procedures?

I have a table of data like this, and one procedure is going to populate data in it like this,
DECLARE #ClaimChanges TABLE (
ChangeType NVARCHAR(10)
,contract_id int NOT NULL
,dispatch_id int NOT NULL
,dispatch_claim_id int NOT NULL
,item_no VARCHAR(100) NULL
,old_units VARCHAR(100) NULL
);
I would then like to pass that data to a different stored procedure which would be defined like this,
CREATE procedure [dbo].[ct_audit_oncost](
#table TABLE readonly,
#OutValue nvarchar(255) = null output
)
as
-- some stuff
go
I gather this is not possible as I am getting an error,
Incorrect syntax near the keyword 'TABLE'.
Table valued parameters are supported since 2008 version.
However, you can't just pass any table you want, first you need to define a user defined table type:
CREATE TYPE dbo.MyUDT as TABLE
(
ChangeType NVARCHAR(10)
,contract_id int NOT NULL
,dispatch_id int NOT NULL
,dispatch_claim_id int NOT NULL
,item_no VARCHAR(100) NULL
,old_units VARCHAR(100) NULL
)
Then you can use it in your stored procedure:
CREATE procedure [dbo].[ct_audit_oncost]
(
#table dbo.MyUDT readonly,
#OutValue nvarchar(255) = null output
)
as
-- some stuff
go
Please note you should also use it to declare the table to send to the database:
DECLARE #Out nvarchar(255)
DECLARE #ClaimChanges as dbo.MyUdt
INSERT INTO #ClaimChanges (ColumnsList) VALUES (ValuesList)
EXEC ct_audit_oncost #ClaimChanges, #Out output

Using Salts in Hashing

Say I have a table like this:
create table [User] (ID int identity not null,EmailAddress varchar(50), [Password] nvarchar(128),primary key (ID))
My reading is telling me that I should add a salt to the table:
alter table [user] add salt nvarchar(128)
If a user creates an account in the application then I believe the SQL should be as follows:
declare #RandomNumber as varchar(32)
set #RandomNumber=rand();
insert into [user] (emailaddress,[password],salt) values ('test#hotmail.co.uk',hashbytes('sha2_256','passwordvalue' + #RandomNumber),#RandomNumber)
Then lets say the user wants to authenticate. The SQL would be like this:
select * from [user] where [password] = hashbytes('sha2_256','passwordvalue' + salt)
The last SQL statement returns nothing, which proves it is incorrect.
Use varchar and not nvarchar, there convertion probleme or declare #RandomNumber as nvarchar(32) like this:
declare #RandomNumber as nvarchar(32)
set #RandomNumber=rand();
insert into [user] (emailaddress,[password],salt) values ('test#hotmail.co.uk',hashbytes('sha2_256','passwordvalue' + #RandomNumber),#RandomNumber)

How to create a stored procedure on SQL server 2014 for create table operation?

I am using SQL Server 2014 and and facing a problem In creating stored procedure. Can anybody tell me how I create a stored procedure for create table operation?
example of this store procedure
CREATE PROCEDURE spCreatetable
AS
BEGIN
Declare #createtablestr varchar(max)
set #createtablestr ='CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
) '
excute #createtablestr ;
END

Turn SQL Server table variable into real table

I have a temporary table created with:
DECLARE #tbl_fk_errors TABLE (
id int Primary Key IDENTITY(1,1)
, tbl_id int
, tbl_name nvarchar(128)
, tbl_record_id int
, tbl_field nvarchar(20)
, tbl_field_value nvarchar(20)
, jnktn_field_value nvarchar(20)
, jnktn_field nvarchar(20)
, jnktn_tblname nvarchar(128)
That table will be filled in my script. Now I want to output that table into a file and found xp_cmdshell which can do that. But that stored procedure can't take my table var because it opens a new connection to the DB (because of the "bcp").
I need to temporary save that table into my DB (and drop it after, but that's no big deal then).
How can I quickly save a table stored in a table variable as real table in my DB?
select *
into table --destination
from #table --source
As a bonus, wrap a transaction around this to make sure you don't leave a "dangling" table in case of an error. DDL is transactional in SQL Server. On rollback, the table will be removed.