Related
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'm inserting datatable using stored procedure and created a type table before,
the query is i want to get back all the 'ProdID' that has been inserted in this session.
for the single insertion i can get the scope identity but i want to get all for the recent insertion.
Thanks in advance.
[dbo].[sp_Isert] (#dt_Product Product_Table READONLY, #ProdID int out)
AS
INSERT into tblProduct (Name,Batch,Qty,ExpDate)
SELECT Name, Batch, Qty, ExpDate
FROM #dt_Product;
set #ProdID = Scope_Identity( )
select Scope_Identity( ) ProdID
Do not use scope_identity() - use the output clause instead.
Note that SQL Server does not support table valued parameters as out parameters, meaning the only way to return a record set from a stored procedure is either by using the output clause (not into table) or by executing a select statement.
Also, do not use the sp prefix for stored procedured.
Microsoft is using this prefix for system procedues, so you might get a name collision.
ALTER PROCEDURE [dbo].[stp_Isert] (#dt_Product Product_Table READONLY)
AS
INSERT into tblProduct (Name,Batch,Qty,ExpDate)
OUTPUT Inserted.Id -- This will return a recordset with the inserted ids to the calling application.
SELECT Name, Batch, Qty, ExpDate
FROM #dt_Product;
Update
I've made a sample script for you to check. When I'm running this on my SQL Server instance, I get the expected results:
CREATE TABLE tbl_TestOutputClause (Id int identity(1,1), Col int );
GO
CREATE TYPE udt_TestOutputClauseIntegers AS TABLE (Value int);
GO
CREATE PROCEDURE stp_TestOutputClauseInsert (#Values dbo.udt_TestOutputClauseIntegers READONLY)
AS
INSERT INTO tbl_TestOutputClause(Col)
OUTPUT INSERTED.Id
SELECT Value
FROM #Values;
GO
CREATE PROCEDURE stp_TestOutputClauseGetInsertedValues
AS
DECLARE #Ids AS TABLE (Id int);
DECLARE #Vals dbo.udt_TestOutputClauseIntegers;
INSERT INTO #Vals (Value) VALUES (1), (2), (3);
INSERT INTO #Ids
EXEC stp_TestOutputClauseInsert #Vals;
-- should return three rows with the values 1, 2 and 3.
SELECT *
FROM #Ids;
GO
EXEC stp_TestOutputClauseGetInsertedValues;
-- clean up
DROP TABLE tbl_TestOutputClause;
DROP PROCEDURE stp_TestOutputClauseInsert;
DROP PROCEDURE stp_TestOutputClauseGetInsertedValues
DROP TYPE udt_TestOutputClauseIntegers;
I have the following table where ProspectCode is Identity Not Null
Table LeadMastersNew
ProspectCode int
CompanyName nvarchar(50)
PersonName nvarchar(50)
Designation nvarchar(50)
Number nvarchar(50)
Number2 nvarchar(50)
Emailaddress nvarchar(50)
Address nvarchar(MAX)
Address2 nvarchar(MAX)
CityName nvarchar(50)
State nvarchar(50)
PinNumber nvarchar(50)
Product nvarchar(50)
RemarkNote nvarchar(MAX)
The issue which I am facing lately is that when I am storing records to the above table using stored procedure,ProspectCode is always set to 0 for all the rows that I add.I have 160 Records in the above table,But when I add new Record, its ProspectCode is set to 0 for all the record that I add.
Stored Procedure
ALTER Procedure [dbo].[Proc_InsertLeads]
#ProspectCode nvarchar(50),#CompanyName nvarchar(50),#PersonName nvarchar(50),#Designation nvarchar(50),#Number nvarchar(50),
#Number2 nvarchar(50),#Emailaddress nvarchar(50),#Address nvarchar(MAX),#Address2 nvarchar(MAX),
#CityName nvarchar(50),#State nvarchar(50),#PinNumber nvarchar(50),#Product nvarchar(50),#RemarkNote nvarchar(MAX)
AS
BEGIN
SET IDENTITY_INSERT LeadMastersNew ON;
INSERT INTO LeadMastersNew
(ProspectCode,CompanyName,PersonName,Designation,Number,Number2,Emailaddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote)
VALUES(#ProspectCode,#CompanyName,#PersonName,#Designation,#Number,#Number2,#Emailaddress,#Address,#Address2,#CityName,#State,#PinNumber,#Product,#RemarkNote)
INSERT INTO LoggerLeadMasters
(ProspectCode,CompanyName,PersonName,Designation,Number,Number2,Emailaddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote,Activity,ActivityTime)
VALUES(#ProspectCode,#CompanyName,#PersonName,#Designation,#Number,#Number2,#Emailaddress,#Address,#Address2,#CityName,#State,#PinNumber,#Product,#RemarkNote,'New Record Added',getdate())
SET IDENTITY_INSERT LeadMastersNew OFF;
END
EXEC Proc_InsertLeads'ABc','Mr abc','MD','PhoneNumber','','abc#abcindia.com','xyz','','Mumbai','Maharashtra','400059','Abc', 'Abc'
Sets ProspectCode to 0
Can anyone help me to fix this issue? Do I have to change my stored Procedure or Table Schema?
Thanks
If you want an identity column to assign numbers automatically, the thing you really shouldn't be doing is setting IDENTITY_INSERT to ON. Turning that setting on says to SQL Server "trust me, I'll provide the values in the identity column".
You probably want code something like:
BEGIN
DECLARE #NewID int
INSERT INTO LeadMastersNew
(/*ProspectCode,*/CompanyName,PersonName,Designation,Number,Number2,Emailaddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote)
VALUES(/*#ProspectCode,*/#CompanyName,#PersonName,#Designation,#Number,#Number2,#Emailaddress,#Address,#Address2,#CityName,#State,#PinNumber,#Product,#RemarkNote)
SET #NewID = SCOPE_IDENTITY()
INSERT INTO LoggerLeadMasters
(ProspectCode,CompanyName,PersonName,Designation,Number,Number2,Emailaddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote,Activity,ActivityTime)
VALUES(#NewID ,#CompanyName,#PersonName,#Designation,#Number,#Number2,#Emailaddress,#Address,#Address2,#CityName,#State,#PinNumber,#Product,#RemarkNote,'New Record Added',getdate())
END
This may not work exactly first time since I can't reconcile the code you've shown with how you're calling it. If ProspectCode is indeed an int column and you're actually trying to insert an nvarchar value of 'Choice Brokers', you should be getting an error.
you don't need identity_insert if you set autoincrement
remove rows
SET IDENTITY_INSERT LeadMastersNew ON;
SET IDENTITY_INSERT LeadMastersNew OFF;
and it should work
Here ProspectCode must set to Auto-increment since it is identity column for table. So basically in store procedure you must remove input parameter #ProspectCode and while inserting new row don't ON OFF IDENTITY_INSERT on table, final store procedure look like:
ALTER Procedure [dbo].[Proc_InsertLeads]
#CompanyName nvarchar(50),#PersonName nvarchar(50),#Designation nvarchar(50),#Number nvarchar(50),
#Number2 nvarchar(50),#Emailaddress nvarchar(50),#Address nvarchar(MAX),#Address2 nvarchar(MAX),
#CityName nvarchar(50),#State nvarchar(50),#PinNumber nvarchar(50),#Product nvarchar(50),#RemarkNote nvarchar(MAX)
AS
BEGIN
DECALRE #ID INT = 0
INSERT INTO LeadMastersNew
(CompanyName,PersonName,Designation,Number,Number2,Emailaddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote)
VALUES(#CompanyName,#PersonName,#Designation,#Number,#Number2,#Emailaddress,#Address,#Address2,#CityName,#State,#PinNumber,#Product,#RemarkNote)
SET #ID = SCOPE_IDENTITY()
INSERT INTO LoggerLeadMasters
(ProspectCode,CompanyName,PersonName,Designation,Number,Number2,Emailaddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote,Activity,ActivityTime)
VALUES(#ID,#CompanyName,#PersonName,#Designation,#Number,#Number2,#Emailaddress,#Address,#Address2,#CityName,#State,#PinNumber,#Product,#RemarkNote,'New Record Added',getdate())
END
Here I am using #ID parameter to find newly inserted ProspectCode in table LeadMastersNew which will used in table LoggerLeadMasters as ProspectCode.
How can I pass a list of column data into a stored procedure?
My stored procedure is
ALTER PROCEDURE [dbo].[Register]
#Id int,
#Name nvarchar(50)
AS
BEGIN
BEGIN TRY
INSERT INTO dbo.Group (Id, Name)
VALUES(#Id, #Name)
SELECT 0
END TRY
BEGIN CATCH
SELECT -1
END CATCH
END
GO
I want pass like this data for insert into this table
#Id = 1,2,3,4,5
#Name = 'test1,test2,test3,test4,test5'
and result like this
Id Name
1 test1
2 test2
3 test3
4 test4
5 test5
A "list" or "array" in SQL Server is ..... a table. So if you're on SQL Server 2008 or newer (you didn't specify), then use the table-valued parameter feature of SQL Server to pass a table of value to your stored procedure
-- Create a table type to match your input parameters
CREATE TYPE IdNameTable AS TABLE
( ID INT, Name NVARCHAR(50) );
GO
-- change your stored procedure to accept such a table type parameter
ALTER PROCEDURE [dbo].[Register]
#Values IdNameTable READONLY
AS
BEGIN
BEGIN TRY
INSERT INTO dbo.Group (Id, Name)
-- get the values from the table type parameter
SELECT
Id, Name
FROM
#Values
SELECT 0
END TRY
BEGIN CATCH
SELECT -1
END CATCH
END
GO
See the extensive and freely available SQL Server Books Online documentation for more details on the table-valued parameter feature and how to use it
If you want to use this from T-SQL, use this code:
-- declare a variable of that table type
DECLARE #InputTable IdNameTable
-- insert values into that table variable
INSERT INTO #InputTable(ID, Name)
VALUES (1, 'Test 1'), (2, 'Test 2')
-- execute your stored procedure with this table as input parameter
EXECUTE [dbo].[Register] #InputTable
If you want to use this from C# or VB.NET, see Michael Edenfield's link in comments.
create procedure InsertQuestionEntry
#round_name varchar(40),
#question varchar(100),
#answer varchar(40),
#option1 varchar(20),
#option2 varchar(30),
#option3 varchar(30)
as
begin
insert into QuestionEntry(Question,Round_Name) values(#question,#round_name);
declare #quesion_id int
exec #quesion_id= select Question_ID from QuestionEntry;
insert into Answer(Question_ID,Answer,Option1,Option2,Option3) values(#quesion_id,#answer,#option1,#option2,#option3);
end
Here I want to retrieve the Question_ID from table QuestionEntry and use that Question_ID to another table Answer
But this didn't work.
So how can I use above way?
please help me
Instead of
insert into QuestionEntry(Question,Round_Name) values(#question,#round_name);
declare #quesion_id int
exec #quesion_id= select Question_ID from QuestionEntry;
use the following:
DECLARE #quesion_id int
INSERT INTO QuestionEntry(Question,Round_Name) values(#question,#round_name)
SET #quesion_id = SCOPE_IDENTITY()
You should not use "exec" there.
What exec does is:
Executes a command string or character
string within a Transact-SQL batch, or
one of the following modules: system
stored procedure, user-defined stored
procedure, scalar-valued user-defined
function, or extended stored
procedure.
You should use "set" or "select" instead of exec.
SET can only assign one variable at
a time, SELECT can make multiple
assignments at once. When assigning
from a query if there is no value
returned then SET will assign
NULL, where SELECT will not make
the assignment at all (so the variable
will not be changed from it's previous
value)
You can find more info about when to use SET or SELECT here: SET vs SELECT when assigning variables
Sample:
set #quesion_id = (select Question_ID from QuestionEntry)
select #quesion_id = (select Question_ID from QuestionEntry)
But that's also wrong way to get identity value from inserted record. If you have N users execute a same procedure at a same time it can happen that you will get wrong value (from last inserted record).
To do this properly you should use ##IDENTITY or even better SCOPE_IDENTITY(). More info: here.
After INSERT you can simply call:
SELECT #quesion_id = ##IDENTITY
--or
SELECT #quesion_id = SCOPE_IDENTITY()
Also, check your Question_ID is configured properly. It should be set to auto increment.
Sample:
Question_ID int IDENTITY(1,1)PRIMARY KEY CLUSTERED
The 1's following the IDENTITY keyword indicate the SEED number (value for first record in table) and increment property (0 or 1).
If your server's version is SQL Server 2005 or higher, you could also try something like this:
create procedure InsertQuestionEntry
#round_name varchar(40),
#question varchar(100),
#answer varchar(40),
#option1 varchar(20),
#option2 varchar(30),
#option3 varchar(30)
as
begin
insert into QuestionEntry(Question,Round_Name)
output inserted.Question_ID, #answer, #option1, #option2, #option3
into Answer (Question_ID, Answer, Option1, Option2, Option3)
values(#question,#round_name);
end