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
35 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2012 r2 - Make Temp Table Query Faster 5 50
export sql results to csv 6 40
Master DB with Masterkey 1 34
SQL Recursion 6 20
I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 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