Solved

How would you convert the following SQL Statement into a stored procedure using passed in parameters with SQL Server 2008?

Posted on 2016-10-07
2
32 Views
Last Modified: 2016-10-08
How would you convert the following SQL Statement into a stored procedure in SQL Server 2008?

The fields starting with "str" would be passed in as parameters:



INSERT into dbo.tbl_CSL_ImportMonthlyChecks (bankname, amt1, banknumber, amt2, datefile, dateimported, importedby)
VALUES ((SELECT bankDescr from dbo.tbl_CSL_Bank WHERE bankID = strBank), strAmt1, strBank, strAmt2, strFiledate, strImportdate, strImportby);
0
Comment
Question by:zimmer9
2 Comments
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41834655
Here it is

CREATE PROC ProcName
AS
BEGIN

	SET NOCOUNT ON;

	INSERT INTO dbo.tbl_CSL_ImportMonthlyChecks (bankname, amt1, banknumber, amt2, datefile, dateimported, importedby)
	VALUES ((SELECT bankDescr from dbo.tbl_CSL_Bank WHERE bankID = strBank), strAmt1, strBank, strAmt2, strFiledate, strImportdate, strImportby)

END

Open in new window

0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41835128
This should give you a start.  Couple of things...
  • Note the @columnname use of parameters in the CREATE PROC statement, then in the SELECT clause.
  • In an INSERT statement if any of the values inserted come from a table then use SELECT instead of VALUES.
  • Are all of these parameters string values, or is Amt numeric and the dates dates?  I assumed the latter, and changed the prefixes to match.  If they are all strings then you'll want to change back to strings plus do a CAST(column_name as varchar(?)) in the SELECT.
  • The columns in the INSERT didn't seem to match the columns in the SELECT, so eyeball these and make sure they line up. Especially bankID = strBank as ID implies numeric and str implies string.

CREATE PROC YourProc(@strBank varchar(100), @Amt numeric(9,2), @dtFileDate date, @dtImportDate date, @strImportBy varchar(100)) AS 

/*
Meaningful code comments go here
zimmer9  2016-10-08  With a little help from https://www.experts-exchange.com/questions/28975167/How-would-you-convert-the-following-SQL-Statement-into-a-stored-procedure-using-passed-in-parameters-with-SQL-Server-2008.html
*/

INSERT INTO dbo.tbl_CSL_ImportMonthlyChecks (bankname, amt1, banknumber, amt2, datefile, dateimported, importedby)
SELECT bankDescr, @Amt, BankNumber, @Amt, @dtFileDate, @dtImportDate, @strImportBy
FROM dbo.tbl_CSL_Bank 
WHERE bankID = @strBank
GO

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question