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
36 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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