If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Stored Procedure to solve this problem !!!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-08-10, 09:24
Abrahm75 Abrahm75 is offline
Registered User
 
Join Date: Feb 2009
Location: Sweden
Posts: 19
Exclamation Stored Procedure to solve this problem !!!

Hi there,
i have this code with c#.net, i need to use stored procedure to do the same work!!!

Code:
SqlCommand comm1;
SqlConnection conn1;
string connectionString1 = ConfigurationManager.ConnectionStrings["DBConnectionString1"].ConnectionString;
conn1 = new SqlConnection(connectionString1);
string sqlcomm1 = "INSERT INTO customer (name, firstname, tele, mobil, fax, email, personno)" +
"SELECT DISTINCT contactperson, Name, Tele, Mobil, Fax, email, PersonNo FROM tem5"+" WHERE (Seid ='" + susid + "')";
comm1 = new SqlCommand(sqlcomm1, conn1);
conn1.Open();
comm1.ExecuteNonQuery();
conn1.Close();
what i need how to make the above sql statement with Insert, Select and Where as stored procedure ???

how i can call the stored procedure from c#.net programming to pass the "susid" ???

thanks alot in advance ...
Reply With Quote
  #2 (permalink)  
Old 03-08-10, 13:11
PMASchmed PMASchmed is offline
Registered User
 
Join Date: Jun 2004
Location: Long Island
Posts: 696
-- Usage exec usp_customer @Seid = 11
create procedure usp_customer
(@Seid int)
as
INSERT INTO customer (name, firstname, tele, mobil, fax, email, personno)
SELECT DISTINCT contactperson, Name, Tele, Mobil, Fax, email, PersonNo FROM tem5 WHERE (Seid = @Seid)

I think to exec, something like this:

SqlCommand comm1;
SqlConnection conn1;
string connectionString1 = ConfigurationManager.ConnectionStrings["DBConnectionString1"].ConnectionString;
conn1 = new SqlConnection(connectionString1);
string sqlcomm1 = "exec usp_customer @Seid = " + susid;
comm1 = new SqlCommand(sqlcomm1, conn1);
conn1.Open();
comm1.ExecuteNonQuery();
conn1.Close();

Last edited by PMASchmed; 03-08-10 at 13:16.
Reply With Quote
  #3 (permalink)  
Old 03-10-10, 07:45
Abrahm75 Abrahm75 is offline
Registered User
 
Join Date: Feb 2009
Location: Sweden
Posts: 19
Hi PMASchmed,
i don't know but it doesn't work!!!

any suggestion !!!
Reply With Quote
  #4 (permalink)  
Old 03-10-10, 17:59
PMASchmed PMASchmed is offline
Registered User
 
Join Date: Jun 2004
Location: Long Island
Posts: 696
Name the columns in the select to match the insert.
Reply With Quote
  #5 (permalink)  
Old 03-11-10, 04:57
Abrahm75 Abrahm75 is offline
Registered User
 
Join Date: Feb 2009
Location: Sweden
Posts: 19
yes, i'll try that ... thanks
Reply With Quote
  #6 (permalink)  
Old 03-11-10, 07:38
PracticalProgram PracticalProgram is offline
Registered User
 
Join Date: Sep 2001
Location: Chicago, Illinois, USA
Posts: 551
Quote:
Originally Posted by Abrahm75 View Post
Hi PMASchmed,
i don't know but it doesn't work!!!

any suggestion !!!
What do you mean that it doesn't work?

Can you create the stored procedure without error?

Did you test the stored procedure within itself?

Whenever I write a stored procedure, I use the following organization:

Code:
create procedure usp_customer

@Seid int

as

set nocount on

/* TEST SECTION
usp_customer @Seid=11 with recompile
declare @Seid int
set @Seid=11
--*/

INSERT  
INTO    customer
SELECT
DISTINCT
        contactperson name
        ,Name firstname
        ,Tele
        ,Mobil
        ,Fax
        ,email
        ,PersonNo
FROM    tem5
WHERE   Seid=@Seid
If you wipe from the "declare" downward you can test your code and make sure the query itself is working.

Did you give the stored procedure execute permissions for the user who will be accessing the code?
__________________
Ken

Maverick Software Design

(847) 864-3600 x2
Reply With Quote
  #7 (permalink)  
Old 03-11-10, 08:50
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
The line comment before your section end comment tag is a nifty idea. I'll definitely use that!
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
Reply

Tags
.net, c#.net, sql server 2008, stored procedure

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On