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 > Store Procedure Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-20-12, 09:36
buzmay buzmay is offline
Registered User
 
Join Date: Jan 2012
Posts: 44
Store Procedure Help

Hi guys,

I have a code that is working, and want to create a store procedure in order to put the information of the running code into a new table.

Does anyone can help me on this, I have never used store procedures.

Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 01-20-12, 13:00
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,447
I'm not sure to understand. To create a stored procedure, you use a SQL script such as:
Code:
USE <YourDatabaseName>
GO
CREATE PROCEDURE <YourProcedureName>
(
	<Optional list of parameters>
)
AS
BEGIN
    <Your code goes here>
END
Ex:
Code:
USE [Sales]
GO
CREATE PROCEDURE [dbo].[DeleteOrderRecord]
(
	@LCF_DataPointer INT 
)
AS
BEGIN
	DECLARE @CF_SysCounter bigINT
	DECLARE @CF_NumLCFLines bigINT
	SET @CF_SysCounter = (SELECT TOP 1 CF_Datum FROM dbo.LCF_Data
		WHERE SysCounter = @LCF_DataPointer)
	DELETE FROM dbo.LCF_Data
		WHERE SysCounter = @LCF_DataPointer
	SET @CF_NumLCFLines = (SELECT TOP 1 SysCounter FROM LCF_Data
		WHERE CF_Datum = @CF_SysCounter)
	IF @CF_NumLCFLines IS NULL
		DELETE FROM dbo.CF_Data WHERE SysCounter = @CF_SysCounter
	RETURN @CF_NumLCFLines
END
__________________
Have a nice day!
Reply With Quote
  #3 (permalink)  
Old 01-22-12, 09:35
buzmay buzmay is offline
Registered User
 
Join Date: Jan 2012
Posts: 44
This is what I have,

Code:
INSERT INTO EXP_DATA_2
SELECT A.PERIOD, A.ACTUALITY, A.COMPANY, A.CURRENCY, B.CC_ACCOUNT,(B.SPLIT * A.AMOUNT)AMOUNT ,A.EXTDIM1,A.EXTDIM2,    A.EXTDIM3,A.EXTDIM4,A.JOURNAL_TYPE,A.C_COMPANY,A.C_DIM,A.TRANAMOUNT,A.TRANCURR,A.REGION,
           A.TRANSF_DATE
        FROM EXP_DATA AS A,
                CC_SPLIT AS B
WHERE A.COMPANY = B.COMPANY AND A.ACCOUNT = B.FUNC_ACCOUNT
AND A.PERIOD IN ('1102', '1103', '1104')
So to make this as a store procedure will it be like this???

Code:
USE [Database]
GO
CREATE PROCEDURE [dbo].[Procedurename]
(
	@LCF_DataPointer INT 
)
AS
BEGIN
	SELECT A.PERIOD, A.ACTUALITY, A.COMPANY, A.CURRENCY, B.CC_ACCOUNT,(B.SPLIT * A.AMOUNT)AMOUNT ,A.EXTDIM1,A.EXTDIM2,    A.EXTDIM3,A.EXTDIM4,A.JOURNAL_TYPE,A.C_COMPANY,A.C_DIM,A.TRANAMOUNT,A.TRANCURR,A.REGION,
           A.TRANSF_DATE
        FROM EXP_DATA AS A,
                CC_SPLIT AS B
WHERE A.COMPANY = B.COMPANY AND A.ACCOUNT = B.FUNC_ACCOUNT
AND A.PERIOD IN ('1102', '1103', '1104')
END

Thanks in advance
Reply With Quote
  #4 (permalink)  
Old 01-22-12, 12:22
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,447
Not exactly.

1. You need to replace several parameters in my example:
Code:
USE [Database]
Database must be replaced by the name of the database where the procedure will be stored.

Code:
CREATE PROCEDURE [dbo].[Procedurename]
(
	@LCF_DataPointer INT 
)
AS
Procedurename must be replaced by the name you want to give to the stored procedure. Moreover, as you don't use any parameter in it, you must remove the parameters declaration part:
Code:
CREATE PROCEDURE [dbo].[YourProcedureName]

AS
3. This:
Code:
(B.SPLIT * A.AMOUNT)AMOUNT ,
should be:
Code:
(B.SPLIT * A.AMOUNT) AS AMOUNT ,
__________________
Have a nice day!
Reply With Quote
  #5 (permalink)  
Old 01-22-12, 15:03
buzmay buzmay is offline
Registered User
 
Join Date: Jan 2012
Posts: 44
Sindho, many thanks for your help I will try this out tomorrow and let you know. Have a nice day
Reply With Quote
  #6 (permalink)  
Old 01-22-12, 15:33
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,447
You're welcome!
__________________
Have a nice day!
Reply With Quote
  #7 (permalink)  
Old 01-23-12, 04:06
buzmay buzmay is offline
Registered User
 
Join Date: Jan 2012
Posts: 44
Hi, I run the store procedure, based on your answers, I get no errors when running the code, however it does not do anything. I mean I want to run this procedure so it inserts the data into exp_Data2 as the code do, but when I run this procedure, this does not happen.

Do you happen to know why?
Reply With Quote
  #8 (permalink)  
Old 01-23-12, 06:58
buzmay buzmay is offline
Registered User
 
Join Date: Jan 2012
Posts: 44
Stupid question of mine I run the procedure but never executed. Thanks anyway
Reply With Quote
Reply

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