473,657 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using text data type in dynamic sql sproc

hi all

this is my first post to this group, so pls bear with me while i try
to make some sense.

i am trying to create a sproc which uses dynamic sql to target a
particuar table eg. '[dbo].[' + @tableID + '_articles']' and perform
some actions.

i am using @tableID [int] as a passes parameter for the sproc.

everything seems to work fine until i try and manipulate a parameter
which is of text data type.

the error occurs when i try to build the dynamic sql string and append
the text type variable.

eg.

CREATE PROCEDURE [procArticlesIns ert]
(
@siteID [int],
@strShortTitle [varchar](40),
@strLongTitle [varchar](60),
@strShortConten t [text],
@strLongContent [text],
@intSectionID [int],
@intTemplateID [int],
@intStatusID [int]
)

AS

DECLARE @strSQL varchar (1000)
DECLARE @strSiteID varchar (10)
SET @strSiteID = CAST(@siteID AS varchar)

SET @strSQL = ('INSERT INTO [' + @strSiteID + '_articles] ' +
' ( [dateEntered], ' +
' [shortTitle], ' +
' [longTitle], ' +
' [shortContent], ' +
' [longContent], ' +
' [sectionID], ' +
' [templateID], ' +
' [statusID]) ' +
'VALUES ' +
' (' + CAST(GETDATE() AS VARCHAR) + ', ' +
'''' + @strShortTitle + ''', ' +
'''' + @strLongTitle + ''', ' +
'''' @strShortConten t , ' +
' @strLongContent , ' +
CAST(@intSectio nID AS VARCHAR) + ', ' +
CAST(@intTempla teID AS VARCHAR) + ', ' +
CAST(@intStatus ID AS VARCHAR) + ')')
GO

i could cast the text fields (@strShortConte nt , @strLongContent ) to
varchar, but the restriction of 8000 characters will not go down so
nicely.

if anyone has any ideas or alternatives to what i am trying to
achieve, i would love to hear from you.

thanks
adrian.
Jul 20 '05 #1
5 8579
Hi

As @strSQL is only varchar(1000) I am not sure why you think you can append
a text column even if converted to varchar(8000)!

Check out http://www.algonet.se/~sommar/dynamic_sql.html and
http://www.algonet.se/~sommar/dyn-search.html
John

"adrian" <ad****@heywood .com.au> wrote in message
news:19******** *************** ***@posting.goo gle.com...
hi all

this is my first post to this group, so pls bear with me while i try
to make some sense.

i am trying to create a sproc which uses dynamic sql to target a
particuar table eg. '[dbo].[' + @tableID + '_articles']' and perform
some actions.

i am using @tableID [int] as a passes parameter for the sproc.

everything seems to work fine until i try and manipulate a parameter
which is of text data type.

the error occurs when i try to build the dynamic sql string and append
the text type variable.

eg.

CREATE PROCEDURE [procArticlesIns ert]
(
@siteID [int],
@strShortTitle [varchar](40),
@strLongTitle [varchar](60),
@strShortConten t [text],
@strLongContent [text],
@intSectionID [int],
@intTemplateID [int],
@intStatusID [int]
)

AS

DECLARE @strSQL varchar (1000)
DECLARE @strSiteID varchar (10)
SET @strSiteID = CAST(@siteID AS varchar)

SET @strSQL = ('INSERT INTO [' + @strSiteID + '_articles] ' +
' ( [dateEntered], ' +
' [shortTitle], ' +
' [longTitle], ' +
' [shortContent], ' +
' [longContent], ' +
' [sectionID], ' +
' [templateID], ' +
' [statusID]) ' +
'VALUES ' +
' (' + CAST(GETDATE() AS VARCHAR) + ', ' +
'''' + @strShortTitle + ''', ' +
'''' + @strLongTitle + ''', ' +
'''' @strShortConten t , ' +
' @strLongContent , ' +
CAST(@intSectio nID AS VARCHAR) + ', ' +
CAST(@intTempla teID AS VARCHAR) + ', ' +
CAST(@intStatus ID AS VARCHAR) + ')')
GO

i could cast the text fields (@strShortConte nt , @strLongContent ) to
varchar, but the restriction of 8000 characters will not go down so
nicely.

if anyone has any ideas or alternatives to what i am trying to
achieve, i would love to hear from you.

thanks
adrian.

Jul 20 '05 #2
[posted and mailed, please reply in news]

adrian (ad****@heywood .com.au) writes:
i am trying to create a sproc which uses dynamic sql to target a
particuar table eg. '[dbo].[' + @tableID + '_articles']' and perform
some actions.

i am using @tableID [int] as a passes parameter for the sproc.
Could you give the rationale for this design? Would it not be better
to have this "tableiD" as a key in the table?

Dynamically created tables are not in the lines of thinking with the
relational data model.
everything seems to work fine until i try and manipulate a parameter
which is of text data type.


Rather than building a complete string and get lost in a maze of quotes
(do you realize that your procedure will throw up, if some one passes
"O'Brien" in one of the input parameters), use sp_executesql instead.
See http://www.algonet.se/~sommar/dynami...#sp_executesql for
details.
--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
The short answer is that you can't do this in the way you're trying to
- @strSQL is varchar(1000), so the total length of the INSERT
statement including data can't be more than that. Obviously, the text
parameter values alone may be much longer.

The best solution, if possible, is almost certainly to remodel your
tables, putting all the articles into a single table, with SiteID as a
column. By doing this, you wouldn't need any dynamic SQL at all, and
it's also a better data model. I appreciate that this may not be easy
in your environment, depending on how much or how little control you
have over the database, how much existing code you have etc. See this
link:

http://www.algonet.se/~sommar/dynami...tml#Sales_yymm

If you can't change the data model, then one alternative is to build
an INSERT string dynamically in your client application - it's often
easier to do it there than in the database.

Simon

ad****@heywood. com.au (adrian) wrote in message news:<19******* *************** ****@posting.go ogle.com>...
hi all

this is my first post to this group, so pls bear with me while i try
to make some sense.

i am trying to create a sproc which uses dynamic sql to target a
particuar table eg. '[dbo].[' + @tableID + '_articles']' and perform
some actions.

i am using @tableID [int] as a passes parameter for the sproc.

everything seems to work fine until i try and manipulate a parameter
which is of text data type.

the error occurs when i try to build the dynamic sql string and append
the text type variable.

eg.

CREATE PROCEDURE [procArticlesIns ert]
(
@siteID [int],
@strShortTitle [varchar](40),
@strLongTitle [varchar](60),
@strShortConten t [text],
@strLongContent [text],
@intSectionID [int],
@intTemplateID [int],
@intStatusID [int]
)

AS

DECLARE @strSQL varchar (1000)
DECLARE @strSiteID varchar (10)
SET @strSiteID = CAST(@siteID AS varchar)

SET @strSQL = ('INSERT INTO [' + @strSiteID + '_articles] ' +
' ( [dateEntered], ' +
' [shortTitle], ' +
' [longTitle], ' +
' [shortContent], ' +
' [longContent], ' +
' [sectionID], ' +
' [templateID], ' +
' [statusID]) ' +
'VALUES ' +
' (' + CAST(GETDATE() AS VARCHAR) + ', ' +
'''' + @strShortTitle + ''', ' +
'''' + @strLongTitle + ''', ' +
'''' @strShortConten t , ' +
' @strLongContent , ' +
CAST(@intSectio nID AS VARCHAR) + ', ' +
CAST(@intTempla teID AS VARCHAR) + ', ' +
CAST(@intStatus ID AS VARCHAR) + ')')
GO

i could cast the text fields (@strShortConte nt , @strLongContent ) to
varchar, but the restriction of 8000 characters will not go down so
nicely.

if anyone has any ideas or alternatives to what i am trying to
achieve, i would love to hear from you.

thanks
adrian.

Jul 20 '05 #4
hi all thanks for the replies and direction.

the main reason why i chose to use dynamic sql as opposed to inserting a
tableid column was because:

i) i thought there would be decrease in performance if there were alot
of record in one table. (eg if the user runs 100 sites with 1000 record
in each)

ii) i did not really have to change much of the data acces logic other
than add in a siteID to the existing procs.

iii) i figured the information would be stored in a more organised
fashion as each new set of tables will represent a site in a multisite
cms eg. [n_articles]

i now understand that my approach is not as easy (or efficient) as i
first thought, and many unseen traps and future maintenance issues with
the implementation of dyn sql.

firstly i need to ask if my first assumption is true? is there a
relationship b/t # of records in a table and performance of the db when
making references to that table?

I could alternatively create a set of stored procedures for each site
[n_procArticlesI nsert], this way I will not need to use dynamic object
names, and will require the least amount of revision to the data and
business logic. Are there any forseen downsides to this?

thanks.
adrian

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
Adrian Hoess (an*******@devd ex.com) writes:
i) i thought there would be decrease in performance if there were alot
of record in one table. (eg if the user runs 100 sites with 1000 record
in each)
There are occasions when this is a viable concern. When you have some
100 million rows in total. Mainly because you want to spread out the table
on different disks and that. Even if you do this, there is still a way
to use the table as a whole, by uniting the tables in a partitioned view.
But this is an advance feature, and with a mere 100000 rows, don't even
think about it.

SQL Server, as any enterprise RDBMS, is designed to handle large amount
of data. It is not designed to handle tables that are created dynamically.
It can handle it, but as you will find, your programming gets more
complicated. And performance usually decreases.

What is important, though, is to have proper indexes on your tables.
ii) i did not really have to change much of the data acces logic other
than add in a siteID to the existing procs.
The same would apply if you put the siteID as a table column.
iii) i figured the information would be stored in a more organised
fashion as each new set of tables will represent a site in a multisite
cms eg. [n_articles]


I'm not sure that I see the relevance of this. A table is a set, and from
a set you can always define a subset. SELECT * FROM tbl WHERE site = @site
is a table which holds the data for one site.

Say that for some reason you need to do a cross-site search. How are you
going to do that it you have one set of tables per site? With one big
table, this is a snap.
--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
16065
by: ImraneA | last post by:
Hi there Application : Access v2K/SQL 2K Jest : Using sproc to append records into SQL table Jest sproc : 1.Can have more than 1 record - so using ';' to separate each line from each other.
1
2012
by: P | last post by:
Hello, I am having a difficult time updating a record via a stored procedure using the gridview and sqldatasource. I cannot seem to be able to find a way to set everything up so that I can pass the applicable fields in the gridview to a sqldatasource which in turn calls a sproc with the appropriate parameters and data. Any ideas? Thanks in advance,
1
7986
by: Roger Twomey | last post by:
I have a database that I don't want to lock. I decided that before any updates can occur I would check a timestamp value and ensure that nobody else updated before I did (avoiding the 'last update wins' scenario). I have a problem, I can read the Timestamp from the db when I read the record. I currently use the data to pre-fill a form (gee go figure ;) ) and the user changes some values and updates. I don't know what to DO with the...
1
1387
by: skirkby | last post by:
This will be obvious to some - but not me I'm afraid... I am using an SQL data link from my ASP application to a SPROC - this all works fine on standard SELECT statements and JOIN in to a datagrid in my application. Rather than messing around with the code/server resource, I thought I would add a COMPUTE line to the bottom of the SPROC to return a total for one of the columns. This all works fine in Query Analyser - I have a handle on...
10
17927
by: pbd22 | last post by:
Hi. Like the title says - how do i do this? I was given the following example: INSERT INTO TABLE2 SELECT * FROM TABLE1 WHERE COL1 = 'A' The above statement threw the following error:
19
248143
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect this data is essential to any developer. This article is a basic tutorial on how to user HTML Forms, the most common method of data collection. Assumptions - Basic HTML knowledge. - Basic PHP knowledge. HTML Forms A common and simple way of...
0
1722
by: Bhavesh | last post by:
Hello genious people, I m trying to insert a LARGE text from Multiline Textbox into my table of sqlserver2000. I m using vs-2005. Please note that I dont want to store blob data From FILE TO TABLE, like storing IMAGE into DB. I hav searched lots of articles on that but didn't get success.
5
2973
by: Bhavesh | last post by:
Hello genious people, I m trying to insert a LARGE text from Multiline Textbox into my table of sqlserver2000. I m using vs-2005. Please note that I dont want to store blob data From FILE TO TABLE, like storing IMAGE into DB.
1
19805
DebadattaMishra
by: DebadattaMishra | last post by:
Introduction In case of rich applications, you must have observed that a text field behaves like a dynamic combo box. When the user enters some characters in the text field, a popup will come up with a list of values as suggestions to the user. This feature is called auto completion. In case of Eclipse editor, you must have seen while writing java code, if the developers presses "Ctrl+Space" a dynamic list box come up with multiple...
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8502
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.