I’m using Visual Studio with DevExpress Winforms in conjunction with Microsoft SQL Server Manager, and I’m currently trying to write a stored procedure that will activate on a button click, copying the focused row to a different table.
I’ve looked into it and haven’t been able to find a solution that does what I need it to. I would prefer not to use checkboxes if possible, but am open to it.
My current code within the stored procedure is:
CREATE PROCEDURE [dbo].[procedurename]
AS
BEGIN
SET NOCOUNT ON;
IF NOT EXISTS (SELECT * FROM tbl_b)
INSERT INTO tbl_b (column names here)
SELECT DISTINCT (column names here)
FROM tbl_a
END
I’ve been able to successfully call this procedure when testing the program with the button, and it functions mostly as intended, moving the data from one table to the other with no duplicates, however it moves the entire table when I only want to move a focused row.
I’m using gridView for this, and I’ve tried solutions that use DataGridView that don’t seem to work.
This is a desktop based program.
You need a parameter in your stored procedure that holds an identification of the row you want to copy
CREATE PROCEDURE [dbo].[procedurename] (#ID int)
AS
BEGIN
SET NOCOUNT ON;
IF NOT EXISTS (SELECT * FROM tbl_b)
INSERT INTO tbl_b (column names here)
SELECT DISTINCT (column names here)
FROM tbl_a
where tbl_A.PrimaryKeyColumn = #ID
END
In the Click event of your button you could then do something like this
var id = myGridView.GetRowCellValue(myGridView.FocusedRowHandle, "YourPrimaryKeyName");
and now you can use this id variable to pass to your stored procedure
However, you do have another problem in your stored procedure
IF NOT EXISTS (SELECT * FROM tbl_b)
This check will never allow you to do your insert once there is 1 or more rows in that table.
I think you need something like
IF NOT EXISTS (SELECT 1 FROM tbl_b where tbl_b.PrimaryKey = #ID)
This question already has an answer here:
Column <column name> does not belong to table
(1 answer)
Closed 4 years ago.
The thing is in my view employee page I have a option called edit where it goes to new page to edit the details of employee here.I am using this line to get the data from sql server to text box and it's showing this error, I have cross checked the column name hell lot of times I don't know where the error is. I have been using a stored procedure to get value from the database
I have all the process for this none helped me.
txtrcountry.Text = dtst.Tables[0].Rows[0]["RCountry"].ToString();
This is my stored procedure which is used to insert edited data to database
USE [PMS v1.0]
GO
/****** Object: StoredProcedure [dbo].[editemp] Script Date: 15-02-2019 09:54:19 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[editemp](#empid int )
as
begin
select Employeeid,firstname,lastname,Gender,convert(varchar(20),(convert(date,DateofBirthday,105))) as DateofBirthday,Mobilenumber,
Alternatenumber,Emailid,AlternateEmail,Fathername,
Mothername,AadhaarCardNo,PanCardNo,PassportNo,UserName,Password,RAddressLine1,RAddressLine2,RCity,RState,RZipCode,RCountry,
PAddressLine1,PAddressLine2,PCity,PState,PZipCode,PCountry,OAddressLine1,
OAddressLine2,OCity,OState,OZipCode,OCountry from newemployee with(nolock) where Employeeid=#empid
select sno,Employeeid,languagename,Expertise from employeelanguges with(nolock) where Employeeid=#empid
end
This is another stored procedure which is used to get the data from database
USE [PMS v1.0]
GO
/****** Object: StoredProcedure [dbo].[anotherpageupdateemp] ScriptDate: 15-02-2019 09:57:36 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[anotherpageupdateemp]
(
#Employeeid int,#firstname nvarchar(20),#lastname nvarchar(40),#Gender nvarchar(10),#DateOfBirth nvarchar(20),
#Fathername nvarchar(20),#Mothername nvarchar(20),
#Emailid nvarchar(20),#AlternateEmail nvarchar(20),#AadhaarCardNo nvarchar(20),
#PanCardNo nvarchar(20),#PassportNo nvarchar(20),#UserName nvarchar(20),
#Password nvarchar(20),#Mobilenumber nvarchar(20),#Alternatenumber nvarchar(20),#RAddressLine1 nvarchar(40),
#RAddressLine2 nvarchar(40),#RCity nvarchar(30),#RState nvarchar(30),#RZipCode nvarchar(20),#RCountry nvarchar(30),
#PAddressLine1 nvarchar(30),#PAddressLine2 nvarchar(30),
#PCity nvarchar(20),#PState nvarchar(20),#PZipCode nvarchar(20),#PCountry nvarchar(30),#OAddressLine1 nvarchar(30),#OAddressLine2 nvarchar(30),#OCity nvarchar(20),
#OState nvarchar(20),#OZipCode nvarchar(20),#OCountry nvarchar(30)
)
as
begin
update newemployee set firstname=#firstname,lastname=#lastname,Gender=#Gender,DateofBirthday=#DateOfBirth,Fathername=#Fathername,Mothername=#Mothername,
Emailid=#Emailid,AlternateEmail=#AlternateEmail,AadhaarCardNo=#AadhaarCardNo,PanCardNo=#PanCardNo,PassportNo=#PassportNo,UserName=#UserName,
Password=#Password,Mobilenumber=#Mobilenumber,Alternatenumber=#Alternatenumber,RAddressLine1=#RAddressLine1,
RAddressLine2=#RAddressLine2,RCity=#RCity,RState=#RState,RZipCode=#RZipCode,RCountry=#RCountry,PAddressLine1=#PAddressLine1,PAddressLine2=#PAddressLine2,
PCity=#PCity,PState=#PState,PZipCode=#PZipCode,PCountry=#PCountry,OAddressLine1=#OAddressLine1,OAddressLine2=#OAddressLine2,OCity=#OCity,OState=#OState,OZipCode=#OZipCode,OCountry=#OCountry
where Employeeid=#Employeeid
end
txtrcountry is the textbox id and RCountry is the column name I should get the details of the country in the textbox.
Your number of parameters in C# side isn't same as the stored procedure. Your parameters should correspond like this:
#Employeeid
#firstname
#lastname
#Gender
#DateOfBirth
#Fathername
#Mothername
#Emailid
#AlternateEmail
#AadhaarCardNo
#PanCardNo
#PassportNo
#UserName
#Password
#Mobilenumber
#Alternatenumber
#RAddressLine1
#RAddressLine2
#RCity
#RState
#RZipCode
#RCountry
#PAddressLine1
#PAddressLine2
#PCity
#PState
#PZipCode
#PCountry
#OAddressLine1
#OAddressLine2
#OCity
#OState
#OZipCode
#OCountry
yes I have solved the whole error Thanks #GaganDeep and #Gauravsa for helping me this error is solved
I have passed an extra argument in C# code
cmd.Parameters.AddWithValue("#Role", empenty.Role);
I removed this line which solved my Bug
I working on an MVC product using Database first entity framework approach, I added a few stored procedure to the EDM, but some are returning string instead of the model type. I have deleted the model.edmx, removed the connection string from the web.config file and re- added model1.edmx file to the project, yet, some still have string as return type, I have also created a viewmodel class and use it in place of the string, yet no luck. I want to be able to add procedure to return type of the model.
Stored Procedure in Model1.Context class:
public virtual ObjectResult<string> PreLoadWorkflowType()
{
return((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<string>("PreLoadWorkflowType");
}
I want the generated stored procedure to look like this instead:
public virtual ObjectResult<PreLoadWorkflowType> PreLoadWorkflowType()
{ return((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<PreLoadWorkflowType>("PreLoadWorkflowType");
}
I have been adding stored procedure to the EDM file all the while, i have not experience this before, I will appreciate assistance from anyone who had experienced and resolved this.
Stored Procedure:
USE [databsename]
GO
/****** Object: StoredProcedure [dbo].[PreLoadWorkflowType] Script Date:
1/30/2018 11:46:15 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[PreLoadWorkflowType]
-- Add the parameters for the stored procedure here
--#RequestType varchar(10),
--#WorkFlowType varchar(20)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
select distinct workflow_type AS WorkflowType from V_ui_View where
workflow_type is not null
END
Where V_ui_View is a view
F-ES Sitecore a senior Developer in another platform posted an answer to this, and it resolved the issue. Check the solution below:
In the model browser select your SP and in the properties window click "Return Type" then click the ellipses button to open the "Edit Function Import" dialog.
In the "Returns a collection of" if you want the SP to return instances of an existing entity then select Entities and the relevant one. If the data doesn't match an existing entity then select "Complex", click "Get Column Information", then click "Create New Complex Type" and it'll create a class for you with the relevant properties and amend the code so it now returns ObjectResult
you are selecting only one column value from statement which has varchar/string type, so this ObjectResult returns String.
I have checked that if selecting multiple column value in select statement then it returns ObjectResult and you can usee only required column value
I'm trying to create a stored proc for the first time, but I just can't get it right. So I have a proc which is returning a count from the db, based on the parameters being fed to it. I'm not sure what I'm doing wrong, if the SQL is wrong or if I'm mapping the function incorrectly. Here's my SQL:
USE [AuditTracker_Codegen]
GO
/****** Object: StoredProcedure [dbo].[GetAllFindingsCount] Script Date: 05/12/2016 08:43:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[GetAllFindingsCount](#ReportId int, #Priority int, #IsDeleted bit)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT COUNT(*)
from [dbo].[App_Finding] finding
WHERE finding.ReportId = #ReportId and finding.Priority = #Priority and finding.IsDeleted = #IsDeleted
END
First off, does this SQL query look right? I've executed it in SSMS and it seems to return the correct result.
I then go to VS, update model from database, add the stored procedure.
Then I go to Function Imports in Model Browser, right-click, Add Function Import. Select the stored proc from the dropdown list, click "Get Column Information", then press "Create New Complex Type". Give the thing a name, then press OK. As far as I know, that should be it, right? Or am I wrong?
In my code I then try
var context = new AuditTrackerDatabaseContext();
var findingsOne = context.GetAllFindingsCount_Result(report.ReportId,
(int) PriorityStatus.One, false).ToString();
And that's where I get the error above. When I check mapping details on the edmx for this proc, it says
Maps to GetAllFindingsCount Column
Column1 : Int32 <- Column1
Is anyone able to advise me on what I'm doing wrong here, why it's not working?
You probably need to give the count value a name, like this:
SELECT COUNT(*) AS RESULT_OF_COUNT
from ...
This is my stored procedure.Someone says using TVP i can get ouput i want. But i am new to sql. So please someone help me, how to pass multiple disposeid's and multiple receiveid's which shown below.Right now i need output only using that I can able to understand what is tvp so someone please edit my SP to pass multiple values in it.
USE [Test]
GO
/****** Object: StoredProcedure [dbo].[up_Get_Customers] Script Date: 09/23/2015 19:10:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[up_Get_Customers]
(
#DisposeId int
)
AS
BEGIN
SELECT C1.DisposeDate,C1.DisposeID,C1.ReceiveDate,C1.ReceiveID
FROM Customers C1
LEFT JOIN Customers C2 ON C1.DisposeID=C1.ReceiveID
WHERE #DisposeId IS NULL OR (C1.DisposeID in (#DisposeId,','))
END
GO
Well, this is something new I've learned today, with a tiny amount of Googling I came up with this
https://msdn.microsoft.com/en-us/library/bb510489%28SQL.100%29.aspx
TVP being Table Valued Parameters.
So based on the information in the link:
CREATE TYPE DisposeIdsTable AS TABLE (
DisposeId VARCHAR(50));
GO
and then
USE [Test]
GO
/****** Object: StoredProcedure [dbo].[up_Get_Customers] Script Date: 09/23/2015 19:10:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[up_Get_Customers]
(
#DisposeIds DisposeIdsTable READONLY
)
AS
BEGIN
SELECT C1.DisposeDate,C1.DisposeID,C1.ReceiveDate,C1.ReceiveID
FROM Customers C1
LEFT JOIN Customers C2 ON C1.DisposeID=C1.ReceiveID
WHERE (SELECT COUNT(*) FROM #DisposeIds) = 0
OR
(C1.DisposeID IN (
SELECT DisposeID FROM #DisposeIds
UNION ALL
SELECT ',')))
END
GO
N.B. The code posted above is not tested at all, so may contain mistakes. Also I've made an assumption about the datatype of DisposeId, so that's unlikely to be correct and would need amending to suit your datatype. Also, I've attempted to preserve the existing logic of the query, whatever that may be.
When executing the stored procedure you will instead need to declare a variable of type DisposeIdsTable rather than the text datatype that you're currently using. That table variable will then need to be populated with your IDs.