SQL Server - How to insert values into one table
Asked By Ajay Paritala
10-Feb-12 09:53 AM
How to insert values into one table after checking the condition. If condition is true then it must insert or else it must return a error message..
From front end am password values to one table2
IN table1 :id, title, description
in table2 i have : id , table1id, title,description
So now am passing values into Table2 it must check that Table1id is existing in table 1 or not how do i check the condition ?
Suchit shah replied to Ajay Paritala
that thing you can check it in following way
1) insert the value in table1 and then by using the return statement return that ID
2) now you can check it like below
select Count(*) from table2 where Id = ID ( here the ID is the return ID of inserted
3) now count is greater than 0 ( zero) means row is present and if not return any row then there is no row so based on row is present or not you can perform your operation
Ajay Paritala replied to Suchit shah
I got it
thanks..
but how to set error code for this?
If condition fails it must return nothing how to do this?
declare @id int
DECLARE @condition int
DECLARE @Errorcode int
SET @Errorcode = 0
set @condition = (select id from categories where id = 55)
if @condition > 0
select * from brands;
else
SET @Errorcode = -1
dipa ahuja replied to Ajay Paritala
string conn = "ConnectionString";
SqlConnection sqlcon = new SqlConnection(conn);
sqlcon.Open();
comm = new SqlCommand("INSERT INTO tb2 (s.no, name,class) SELECT s.no, name , class FROM tb1", sqlcon);
comm.ExecuteNonQuery();
sqlcon.Close();
kalpana aparnathi replied to Ajay Paritala
Try this Example code for your solution:
DECLARE @userData TABLE(
name varchar(30) NOT NULL,
oldlocation varchar(30) NOT NULL
);
INSERT @userData
SELECT name, location FROM myTable
INNER JOIN otherTable ON ...
WHERE age>30;
Web Star replied to Ajay Paritala
You just put comple stored proc with output paramter which is return error
Create proc spName
(
@id int,
@Title varchar(10),
@desc varchar(1000),
@ReturnVal varchar(200) out
)
Begin
--here you need to check first that id exist in table1
If exists(Select * from table1 where id = @id)
Begin
--here insert in table2
inser into table2 Values(@id, @Title, @desc)
set @ReturnVal = 'Successfully Inserted'
End
else
Begin
set @ReturnVal = 'Error message here'
End
End
SQL Server Error: 18452 SQL Server Connection failed: SQLState: '28000' SQL Server Error: 18452 [Microsoft][ODBC SQL Server Driver][SQL Server] Login failed for user 'sa'. Reason: Not
Connectivity issue (SQL server error: 53 and 17) SQL Server SQL server 2000 standard edition, Microsoft Windows NT 5.2 (3790) One of my application running Microsoft access database connects to this server. Yesterday I applied a update to the application and now we are not able to
SQL Server Error 5520 SQL Server I am getting the SQL Server Error 5520 when i try to add a FILSTREAM file into a filegroup. I am trying
MSDE on Windows 2003 R2 box, new DL385G6 - Install Fails during SQL Services SQL Server I have been finding that I am having trouble with the Crystal Reports Server XI installation failing when it is dealing with SQL. So, as a thought and in case there was something wrong with my SQL portion of the isntall. I thought ok, I will try installing the actual MSDE application direct from Microsoft. So, I downloaded the MSDE for SQL 2000 (which is msde2000a.exe), set my switches and off to the races. It quit seconds left to the installation and bombed with the same errors as the Crystal Reports Server install. The error is the same whether I try to install MSDE by itself or
Additonal thoughts, you should be able to connect to SQL Server via SQL Server SQL Server Additonal thoughts, you should be able to connect to SQL Server via SQL Server Management Studio. This will provide you with whether or not SQL Server