I have a stored procedure in SQL as below:
CREATE PROCEDURE USP_Select
(#id INT)
AS
SELECT * FROM EMO;
SELECT * FROM student WHERE id = #id;
SELECT * FROM tbl1;
RETURN 0
I am getting data using Entity Framework from that stored procedure using this code:
Modalities context = new Modalities();
context.USP_Select(1);
How can I define which table data gets in my code?
So here how can I get data different tables in code from the stored procedure?
first, add your stored procedure in .edmx file and create a complex type for that stored procedure and you can use an entity. Use the following link to create complex type
Adding stored procedures complex types in Entity Framework
Cant you just pass a parameter to select which table you wanna get data from?
CREATE PROCEDURE USP_Select
#table varchar(50),
#id INT = NULL
AS
BEGIN
IF #table = "EMO"
BEGIN
SELECT * FROM EMO;
END
ELSE IF #table = "student"
BEGIN
SELECT * FROM student WHERE id = #id;
END
ELSE IF #table = "tal1"
BEGIN
SELECT * FROM tbl1;
END
END
Related
I want to create an SQL table using a stored procedure with a dynamic name and dynamic columns. After creating the table in the stored procedure I want to create a view with that dynamic table name and dynamic columns within the stored procedure.
CREATEPROCEDURE [dbo].[Tmp_CloneOfDataset]
#Filter NVARCHAR(500),
#Value NVARCHAR(50)=NULL
AS
BEGIN
SET NOCOUNT ON
DECLARE #GetTemName varchar(200)= RTRIM(LTRIM( SUBSTRING(#Filter,CHARINDEX('into',#Filter)+4,CHARINDEX('from',#Filter)-CHARINDEX('into',#Filter)-4)))
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = #GetTemName)
BEGIN
DECLARE #alreadyExist int =1;
return #alreadyExist
EXECUTE(#Filter)
END
ELSE
BEGIN
EXECUTE(#Filter)
// Here i want to create view after creating table
DECLARE #status varchar(200)='SELECT * from '+ #GetTemName
Execute(#status)
END
END
I have written a stored procedure which executes a stored procedure and the result it gets has to be stored in a local temporary table. The Stored procedure gets created without giving any errors. But when I try to execute stored procedure, it returns the error that the temporary table is invalid object name.
CREATE PROCEDURE .dbo.CalulateETFWeights
-- Add the parameters for the stored procedure here
#CURR_DATE varchar(255),
#ETF_DATE datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--Select max(ETF_DATE) into #ETF_DATE from .dbo.ETF_LIST_V --where ETF_LOAD_DATE = #CURR_DATE
-- Insert statements for procedure here
SELECT TqaSecCode, GlobalSecurity, Cusip
into #tempetftable
from .map.v_get_tqa_security where cusip in (select distinct ETF_CUSIP from .dbo.ETF_LIST_V where ETF_LOAD_DATE = 'Mon Jun 17 14:15:09 BST 2019')
Insert into #tempPriceTable
exec .tqaif.sp_get_ds_price_usd
#sourceTable = '#tempetftable',
#startDate = '20181219',
#endDate = '20181219',
#frequency = 'D'
Insert into .dbo.ETFComponentWeights
Select
C.ETF_CUSIP as W_CAL_CUSIP,
C.STK_IDX as W_CAL_COMP,
C.STK_QUANT as W_CAL_SHARES,
CP.VALUE as W_CAL_PRICE,
(C.STK_QUANT * CP.VALUE_) as W_CAL_MVAL,
(C.STK_QUANT * CP.VALUE_)/SUM(C.STK_QUANT * CP.VALUE) over (partition by C.ETF_CUSIP) as W_CAL_WEIGHT,
#ETF_DATE as W_CAL_DATE
from .dbo.ETF_COMP_V C
inner join (Select E.CUSIP, P.Value_ from #tempPriceTable P inner join #tempetftable E on P.TqaSecCode = E.TqaSecCode) CP
on C.ETF_CUSIP = CP.CUSIP
So the error I get is
Invalid object name '#tempPriceTable'.
I don't understand why is this not working? Can anyone suggest what am I doing wrong here? and why the #tempetftable works fine. But #tempPriceTable here is not working in this scenario?
Syntax:
SELECT TqaSecCode, GlobalSecurity, Cusip
into #tempetftable
creates a new temp table and then inserts data into this new table.
Syntax:
Insert into #tempPriceTable
exec .tqaif.sp_get_ds_price_usd
is a regular "insert into" statement, which adds rows to existing table.
To use this syntax you need to create an empty temp table with correct schema beforehand.
So you need to do something like:
CREATE TABLE #tempPriceTable (your schema)
Insert into #tempPriceTable
exec .tqaif.sp_get_ds_price_usd
I am trying to trigger a stored procedure to take the inserted values into my stored procedure as parameters and it is not letting me.
My table flow goes like this: a patient's history information will be inserted (HISTORY_APPOINTMENTS) and if at the time the patient has a column value of HasSuicidalThoughts = 'Y' I want the trigger to send the inserted patients information into a table I created called SuicideWatchLog.
First I created the table:
/* Table Creation for SuicideWatch Log*/
CREATE TABLE SuicideWatchLog
(
logNum integer IDENTITY(1,1) NOT NULL PRIMARY KEY,
PatientStudy# integer FOREIGN KEY References Patients(PatientStudy#),
PatientName varchar(20),
[Date] date,
Dr# integer FOREIGN KEY References DOCTORS(Dr#),
DaysinStudy integer
)
Next I created the procedure:
CREATE PROCEDURE AddSuicideWatch
#PatientStudy# integer,
#PatientName varchar(20),
#Date date,
#Dr# integer,
#DaysinStudy integer
AS
BEGIN
INSERT INTO SuicideWatchLog(PatientStudy#, Date, Dr#)
(SELECT PatientStudy#, ApptDate, Dr#
FROM APPOINTMENTS
WHERE #PatientStudy# = PatientStudy#
AND #Date = ApptDate
AND #Dr# = Dr#)
INSERT INTO SuicideWatchLog(PatientName, DaysinStudy)
(SELECT PatientFName, datediff(day,StudyStartDate,getdate())
FROM PATIENTS
WHERE #PatientName = PatientFName
AND #DaysinStudy = datediff(day,StudyStartDate,getdate()))
END
Finally I created the trigger:
CREATE TRIGGER SuicidalPatient
ON HISTORY_APPOINTMENT
AFTER INSERT
AS
EXEC AddSuicideWatch(
SELECT (I.PatientStudy#, P.PatientFName, A.ApptDate,
FROM INSERTED I
JOIN APPOINTMENTS A ON I.Appt# = A.Appt#
JOIN PATIENTS P ON I.PatientStudy# = P.PatientStudy#)
I expected this to allow me to send the inserted values into the stored procedure to trigger the creation of the log, but instead I am getting an error that is telling me my parameters aren't being found.
Is this an issue with the select statement, or is it a problem with the procedure itself?
Is this an issue with the select statement, or is it a problem with the procedure itself?
Your stored procedure accepts scalar parameters. You can't pass a whole resultset to it. You can:
1) Integrate the INSERTs directly into the trigger body, eliminating the stored procedure.
2) Open a cursor over the query in the trigger, and loop through the rows, calling the stored procedure fore each one.
3) Declare a User-Defined Table Type matching the query result rows, declare and load an instance of the table type in the trigger body, and change the stored procedure to accept a Table-Valued Parameter.
you cant pass table to sp , but i know 2 ways for that :
1- use user defined type like that :
create type NewTable AS table (PatientStudy# int, PatientFName nvarchar(max), ApptDate date)
and the insert into NewTable Then call sp
declare #TempTable NewTable
insert into #TempTable(PatientStudy# , PatientFName , ApptDate)
select I.PatientStudy#, P.PatientFName, A.ApptDate,
FROM INSERTED I
JOIN APPOINTMENTS A ON I.Appt# = A.Appt#
JOIN PATIENTS P ON I.PatientStudy# = P.PatientStudy#
EXEC AddSuicideWatch( #TempTable)
and of course you should edit your SP :
CREATE PROCEDURE AddSuicideWatch
#Table NewTable
AS
BEGIN
INSERT INTO SuicideWatchLog(PatientStudy#, Date, Dr#)
SELECT PatientStudy#, ApptDate, Dr#
FROM APPOINTMENTS A join #Table T ON
A.PatientStudy# = T.PatientStudy#
A.Date = T.ApptDate
A.Dr# = D.Dr#
INSERT INTO SuicideWatchLog(PatientName, DaysinStudy)
(SELECT PatientFName, datediff(day,StudyStartDate,getdate())
FROM PATIENTS P join #Table T ON
T.PatientName = P.PatientFName
AND A.DaysinStudy = datediff(day,StudyStartDate,getdate()))
END
And the seccond way : just pass the primary key to sp and handle other things in sp
I want to use SQL Server 2014 stored procedure in Report Builder.
ALTER PROCEDURE [dbo].[getCharacterDetails]
#id int
,#name VARCHAR(200) = '' OUTPUT
,#level int = 0 OUTPUT
AS
BEGIN
SELECT
#name = name, #level = level
FROM
dbo.Characters
WHERE
id = #id;
RETURN
END
I want to use #id as an input parameter and name and level as output only parameters.
When I call the procedure using Report Builder I get no data.
When I execute a query:
exec [dbo].[getCharacterDetails] #id= 1;
I also get no result values, only (1 row(s) affected) or Commands completed successfully.
The table Characters contains 1 row with id=1, so the select is correct.
Am I doing it right? Should I be using stored procedures or maybe UDF?
I'm new to SQL Server.
No, this stored procedure definitely doesn't return rows - because you're capturing the values into output parameters.
If you want to return rows - do not assign those columns to variables! Just write the select"as is".
So just this this SELECT statement instead:
SELECT
name, level
FROM
dbo.Characters
WHERE
id = #id;
and your stored procedure will now return a result set of data.
Try this:
ALTER PROCEDURE [dbo].[getCharacterDetails]
#id int
,#name VARCHAR(200) = '' OUTPUT
,#level int = 0 OUTPUT
AS
BEGIN
SELECT
name, level
FROM
dbo.Characters
WHERE
id = #id;
RETURN
END
Try running the stored procedure as:
declare #name VARCHAR(200);
declare #level int;
exec dbo.getCharacterDetails 1, #name output, #level output;
select #name, #level;
You have to provide the arguments and then look at them afterwards. That is how output parameters work.
If you want the stored procedure to return rows, then don't assign the values, or you can do both. The body could be:
SELECT #name = name, #level = level
FROM dbo.Characters
WHERE id = #id;
SELECT #name, #level;
And, for such a simple process, you might want just a view or user defined function.
Your data is cached, delete the data file (rdl.data) it's in the same folder your rdl is in
The name and level parameters serve no purpose. Remove them.
ALTER PROCEDURE [dbo].[getCharacterDetails]
#id int
AS
BEGIN
SELECT
name, level
FROM
dbo.Characters
WHERE
id = #id;
RETURN
END
call it like this:
EXEC [dbo].[getCharacterDetails] #ID
and make sure you map a SSRS parameter to the input parameter #ID in the parameters tab.
If you still don't get anything use SQL Profiler to work out what parameter it's passing in.
I need to pass the parameter (#name) as string 'Alex','david','crowner' to the query in a stored procedure.
The query in the stored procedure looks like this:
select *
from employees
where name in (#name)
The value for #name parameter that would be passed would be something like
'Alex','david','crowner'
How could I handle this in my stored procedure to find the names in a table with IN operator?
In SQL Server 2008 and later, you can use a table valued parameter. In the database, you have to create a table type. For example:
-- Drop old example definitions
if exists (select * from sys.procedures where name = 'TestProcedure')
drop procedure TestProcedure
if exists (select * from sys.types where name = 'TestTableType')
drop type TestTableType
if exists (select * from sys.tables where name = 'TestTable')
drop table TestTable
go
-- Create example table, type and procedure
create table TestTable (id int identity, name varchar(50))
create type TestTableType as table (name varchar(50))
go
insert TestTable values ('Bill'), ('George'), ('Barrack')
go
create procedure dbo.TestProcedure
#List TestTableType readonly
as
select *
from TestTable
where name in
(
select name
from #List
)
go
In C#, you can pass a DataTable as a table-valued parameter:
var listTable = new DataTable();
listTable.Columns.Add("Name", typeof(string));
listTable.Rows.Add("Bill");
listTable.Rows.Add("George");
var listParameter = new SqlParameter();
listParameter.ParameterName = "#List";
listParameter.Value = listTable;
using (var con = new SqlConnection("Server=localhost;Database=test;" +
"User Id=testuser;Password=testpassword;"))
{
var com = con.CreateCommand();
com.CommandText = "dbo.TestProcedure";
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add(listParameter);
con.Open();
using (var read = com.ExecuteReader())
{
while (read.Read())
Console.WriteLine(read["name"]);
}
}
The amount and complexity of code required for even a single table-valued parameter is no complement for the SQL Server designers.
you can pass in a single string to the parameter and inside the body of the stored proc use function(s) like charindex and substring and replace to do what you want
After some research I stumbled over a Code Project thread which was the key to solve the problem. With the help of the above thread I wrote the following stored procedure:
CREATE PROCEDURE [dbo].[myWorkingProcedure]
#inputList nvarchar(MAX)
AS
DECLARE #SetPoint INT
DECLARE #VALUE nvarchar(50)
CREATE TABLE #tempTab (id nvarchar(50) not null)
BEGIN
SET NOCOUNT ON;
WHILE PATINDEX('%,%',#inputList) > 0 <-- Drive loop while commata exist in the input string
BEGIN
SELECT #SetPoint = PATINDEX('%,%',#inputList) <-- Determine position of next comma
SELECT #VALUE = LEFT(#inputList , #SetPoint - 1) <-- copy everything from the left into buffer
SELECT #idList = STUFF(#inputList, 1, #SetPoint, '') <-- throw away the stuff you copied
INSERT INTO #tempTab (id) VALUES (#VALUE) <-- put value in buffer table
END
INSERT INTO #tempTab (id) VALUES (#inputList) <-- insert last value in the input list to buffer
BEGIN
SELECT * FROM myTable
WHERE myColumn IN (SELECT id FROM #tempTab) <-- Do the select
DROP TABLE #tempTab <-- throw buffer table away
END
END
GO