473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Write a Script as Varchar Parameter in Stored Procedure

Hi all,

I paid 3 hours for it but couldn't get succeed.
I'm writing a stored procedure to get a data entry in a table. The problem is that I can't simply use commands "SELECT" or "CASE" directy. First I have to declare a varchar parameter and put all of this in this parameter. (The reason is that the stored procedure is not unique for a table. Year and month, which are parts of the table name are also parameters of my S.P.)
There is NO problem when I don't use "CASE" . e.g.

CREATE PROCEDURE [GetCustomerInfo]
(@Year_1 [char](4),
@Month_2 [char](2),
@CustomerId_3 [int])

AS
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[Customers_'+@Year_1+'_'+@Month_2+']') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
DECLARE @Sql varchar(300)
SET @Sql='
SELECT
[CustomerId],
[CustomerName],
FROM [Customers_'+@Year_1+'_'+@Month_2+']
WHERE
@CustomerId_3='+CAST(@DetectorId_3 AS VARCHAR(10))+' '
EXEC(@Sql)
END

The SP above works.
But when I use a CASE statement to while "select"ing a column, the SP doesn't work properly although there is no syntax error. I still think that the problem is due to syntax.
How can I come along with this? Please help me.
May 22 '07 #1
2 2098
almaz
168 Expert 100+
Replace "DECLARE @Sql varchar(300)" with "DECLARE @Sql varchar(4000)".

If it won't help, please post the script you are trying to run (with CASE statement)
May 22 '07 #2
Replace "DECLARE @Sql varchar(300)" with "DECLARE @Sql varchar(4000)".

If it won't help, please post the script you are trying to run (with CASE statement)
Thank you for your advice. I already tried this but it didn't help. But my problem is solved:) (I don't know how) I just wrote the SProc all over from the beginning.
May 22 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Clay Beatty | last post by:
When you create database diagrams in Enterprise Manager, the details for constructing those diagrams is saved into the dtproperties table. This table includes an image field which contains most of...
6
by: jim_geissman | last post by:
I have encountred situations like this before, but this one has me stumped. I have a pretty simple SP that collects information about residential properties from a large database. First step is...
4
by: Maziar Aflatoun | last post by:
Hi, How do I return a varchar value from a stored procedure? Ex. SELECT GotoURL FROM MarketingCampaigns WHERE CampaignID=@CampaignID I like to return the GotoURL as a string? I don't want to...
10
by: Eric E | last post by:
Hi all, I am using an Access client linked to a PG 7.4 server via ODBC. I have a stored proc on the server that inserts rows into a table.particular table, accomplished via an INSERT within the...
2
by: satish | last post by:
hi, i am a learner of ms -sql server 2000, i had a doubt in stored procedures suppose i have a data base having 20 tables, all the tables have a column named--DATE can we write a store...
4
by: Ranginald | last post by:
Hi, I'm having trouble passing a parameter from my default.aspx page to my default2.aspx page. I have values from a query in a list box and the goal is to pass the "catID" from default.aspx...
4
by: Brian Parker | last post by:
I've been banging my head on this for hours and it's something that should be very very easy. I have a Stored Procedure in a MS-SQL 2000 server that returns a Varchar(40) and I want to grab that...
3
by: Odd Bjørn Andersen | last post by:
Is there a way to check the return status from a stored procedure when called from a OS (Windows) script? I have a stored procedure that does some checking in the database, and the return status...
1
by: sqldba20 | last post by:
Folks: I want to run sp_recompile on a weekly basis. Does anybody have a script / stored procedure which will get all the Stored Procedures and Tables from a database and do a recompile. I wrote...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.