You are on page 1of 8

Q) Why are my insert, update statements failing with the following error? Server: Msg 815 , !

evel 1", State #, !ine 1 String or $inary data would $e trun%ated& 'he statement has $een terminated&

'his error o%%urs, when the length of the value entered $y you into a %har, var%har, n%har, nvar%har %olumn is longer than the ma(imum length of the %olumn& )or e(ample, inserting *)+Q* into a %har, ) %olumn would result in this error& -rofiler is handy in trou$leshooting this error& .f data trun%ation is o/ay with you and you don*t want to see this error, then turn off +0S. W+10.02S $y using the following S3' %ommand: S3' +0S.4W+10.02S 5))& Steps to reprodu%e the pro$lem: 613+'3 '+7!3 My'a$le , -/ey int -1.M+18 938, 6ol1 %har,1:) ) 25 .0S31' .0'5 My'a$le ,-/ey, 6ol1) ;+!<3S ,1, *SQ! Server 6lustering )+Q*) 25 Ma/e sure, you restri%t the length of input, in your front=end appli%ations& )or e(ample, you %ould use the M+>!302'? property of the te(t $o(es in ?'M! forms& 3&g: @.0-<' 0+M3 A B0ameB '8-3A '3>'75> M+>!302'?A :C What is the '=SQ! eDuivalent of ..) ,immediate ifEternary operator) fun%tion of other programming languages? @topC 6+S3 is the eDuivalent of ..) fun%tion& See SQ! Server 7oo/s 5nline for more information& ?ere*s a Dui%/ e(ample: 613+'3 '+7!3 -eople , F.GH int -1.M+18 938, F0ameH var%har, 5) 05' 0<!!, Se( $it 0<!! ) .0S31' .0'5 -eople ,F.GH,F0ameH, Se() ;+!<3S ,1,*Iohn Gy/es*, 1) .0S31' .0'5 -eople ,F.GH,F0ameH, Se() ;+!<3S , ,*Ge$orah 6roo/*, :) .0S31' .0'5 -eople ,F.GH,F0ameH, Se() ;+!<3S ,J,*- S Su$ramanyam*, 0<!!) S3!36' F.GH, F0ameH, 6+S3 Se(

W?30 1 '?30 *Male* W?30 : '?30 *)emale* 3!S3 *0ot spe%ified* 30G +S Se( )15M -eople ?ow to programmati%ally find out when the SQ! Server servi%e started? @topC 3verytime SQ! Server starts, it re%reates the tempd$ data$ase& So, the %reation date and time of the tempd$ data$ase tells us the date and time at whi%h SQ! Server servi%e started& 'his information is stored in the %rdate %olumn of the sysdata$ases ta$le in master data$ase& ?ere*s the Duery to find that out: S3!36' %rdate +S *SQ! Server servi%e started appro(imately at:* )15M master&d$o&sysdata$ases W?313 name A *tempd$* SQ! Server error log also has this information ,'his is more a%%urate) and the error log %an $e Dueried using (p4readerrorlog ?ow to get rid of the time part from the date returned $y 23'G+'3 fun%tion? @topC We have to use the 650;31' fun%tion to strip the time off the date& +ny of the following %ommands will do this: S3!36' S3!36' S3!36' S3!36' 650;31',%har,23'G+'3,),1:1) 650;31',%har,23'G+'3,),1: ) 650;31',%har,23'G+'3,),1:J) 650;31',%har,23'G+'3,),1)

See SQ! Server 7oo/s 5nline for more information on 650;31' fun%tion& ?ow to upload images or $inary files into SQ! Server ta$les? @topC )irst of all, if possi$le, try not to stored images and other $inary files in the SQ! Server ta$les, as they slow things down& .nstead, store a lin/ ,file path) to the file in the ta$les and let your appli%ations dire%tly a%%ess the files& 7ut if you must store these files within SQ! Server, use the te(tEnte(t or image datatype %olumns and %onsider the following options: SQ! Server K&: and ::: %ome with a utility %alled te(t%opy&e(e& 8ou %an lo%ate this file in the 7inn folder under your SQ! Server installation folder& 1un this file from %ommand prompt, and it will prompt you for reDuired input <se the 2et6hun/ and +ppend6hun/ methods of +G5 )ield o$Le%t& MSG0 has e(amples <se the +G5 Stream o$Le%t <se the 7ul/ .nsert .mage utility ,7..) that ships with SQ! Server ::: ,6an $e found at M-rogram )ilesMMi%rosoft SQ! ServerM8:M'oolsMGevtoolsMSamplesM<tils)

?ow to run an SQ! s%ript file that is lo%ated on the dis/, using '=SQ!? @topC 'here*s no dire%t %ommand to read a s%ript file and e(e%ute it& 7ut the isDl&e(e and osDl&e(e %ome in handy when you have to e(e%ute a s%ript file from within '=SQ!& Iust %all any of these e(es using (p4%mdshell and pass the s%ript file name as parameter to it& See SQ! Server 7oo/s 5nline for more information a$out the input parameters of these e(es& ?ere are some Dui%/ e(amples: 3>36 master&&(p4%mdshell *osDl =Svaio =<sa =-Naassds1 =i%:MMySQl&sDl =n* 3>36 master&&(p4%mdshell *isDl =Svaio =<sa =-Naassds1 =i%:MMySQl&sDl =n* See (p4%mdshell in SQ! Server 7oo/s 5nline if you are having permissions pro$lems in getting this te%hniDue to wor/& ?ow to get the %omplete error message from '=SQ! while error handling? @topC <nfortunately, the error handling %apa$ilities of SQ! Server are limited& When an error o%%urs, all you %an get is the error num$er, using the OO31151 glo$al varia$le& 'here is no OO311514M3SS+23 glo$al varia$le to get the error des%ription& )or a %omplete error message, you %an always Duery the master&&sysmessages ta$le using the error num$er, $ut most of these messages have pla%e holders ,li/e Ps, Pl et%&), and hen%e we %an*t get the %omplete error message& ?owever, the %lient appli%ations using an o$Le%t model su%h as 1G5, +G5 have a%%ess to the %omplete error message& ?ow to get the first day of the wee/, last day of the wee/ and last day of the month using '=SQ! date fun%tions? @topC ?ere*s the %ode: G36!+13 OGate datetime S3' OGate A * ::1E:8EJ1* S3!36' G+'3+GG,dd,=,G+'3-+1',dw, OGate) = 1),OGate) +S *)irst day of the wee/* S3!36' G+'3+GG,dd,=,G+'3-+1',dw, OGate) = K),OGate) +S *!ast day of the wee/* S3!36' G+8,G+'3+GG,d, =G+8,G+'3+GG,m,1,OGate)),G+'3+GG,m,1,OGate))) +S *!ast day of the month* ?ow to pass a ta$le name, %olumn name et%& to the stored pro%edure so that . %an dynami%ally sele%t from a ta$le? @topC 7asi%ally, S3!36' and other %ommands li/e G15- '+7!3 won*t let you use a varia$le instead of a hard%oded ta$le name& 'o over%ome this pro$lem, you have to use dynami% sDl& 7ut dynami% SQ! has some disadvantages& .t*s slow, as the dynami% SQ! statement needs to $e parsed everytime it*s e(e%uted& )urther, the user who is e(e%uting the dynami% SQ! string needs dire%t permissions on the ta$les, whi%h defeats the purpose of having stored pro%edures to mas/ the underlying ta$les&

?aving said that, here are some e(amples of dynami% SQ!: ,+lso see sp4e(e%utesDl in SQ! Server 7oo/s 5nline) 613+'3 -156 Grop'a$le O'a$le sysname +S 3>36 ,*G15- '+7!3 * Q O'a$le) 25 3>36 Grop'a$le *My'a$le* 25 613+'3 -156 Sele%t'a$le O'a$le sysname +S 3>36 ,*S3!36' R )15M * Q O'a$le) 25 3>36 Sele%t'a$le *My'a$le* )or a %omplete dis%ussion on the pros and %ons of dynami% SQ! %he%/ out 3rland*s arti%le: 'he %urse and $lessings of dynami% SQ! 3rror inside a stored pro%edure is not $eing raised to my front=end appli%ations using +G5& 7ut . get the error when . run the pro%edure from Query +nalyNer @topC 'his typi%ally happens when your stored pro%edure is returning multiple resultsets and the offending SQ! statement is e(e%uted after returning one or more resultsets& +G5 will not return an error untill it pro%esses all the re%ordsets returned $efore the offending SQ! statement got e(e%uted& So, to get to the error message returned $y your pro%edure& 8ou have to loop through all the re%ordsets returned& +G5 1e%ordset o$Le%t has a method %alled 0e(t1e%ordset, whi%h lets you loop through the re%ordsets& ?aving S3' 0565<0' 50 at the $eginning of the pro%edure also helps avoid this pro$lem& S3' 0565<0' 50 also helps in improving the stored pro%edure performan%e& ?ere*s a sample pro%edure to simulate the pro$lem: 613+'3 -156 'est-ro% +S S3!36' M+>,6ol1) )15M 'est'a$le S3!36' M.0,6ol1) )15M 'est'a$le .0S31' .0'5 'est'a$le ,6ol1, 6ol ) ;+!<3S ,1,*5ra%le and SQ! Server %omparison*) .0S31' .0'5 'est'a$le ,6ol1, 6ol ) ;+!<3S ,1,*?ow to %onfigure SQ! Server?*) == Guppli%ate /ey error o%%urs 25 ?ow to suppress error messages in stored pro%eduresEtriggers et%& using '=SQ!? @topC

.t*s not possi$le to suppress error messages from within '=SQ!& 3rror messages are always returned to the %lient& .f you don*t want your users to see these raw error messages, you should handle them in your front=end appli%ations& )or e(ample, if you are using +G5 from +S- to %onne%t to SQ! Server, you would do something li/e the following: 5n 3rror 1esume 0e(t Set 1s A 6onn&3(e%ute ,B.0S31' .0'5 My'a$le ,1,*?ow to migrate from 5ra%le to SQ! Server*,*7oo/*B) .f 3rr&0um$er @C : 'hen 1esponse&Write ,B3rror o%%urred while inserting new dataB) 5n 3rror 2o'o : ?ow to save the output of a DueryEstored pro%edure to a te(t file using '=SQ!? @topC '=SQ! $y itself has no support for saving the output of DueriesEstored pro%edures to te(t files& 7ut you %ould a%hieve this using the %ommand line utilities li/e isDl&e(e and osDl&e(e& 8ou %ould either invo/e these e(e files dire%tly from %ommand promptE$at%h files or from '=SQ! using the (p4%mdshell %ommand& ?ere are the e(amples: )rom %ommand prompt: osDl&e(e =S 8ourServer0ame =< sa =- se%ret%ode =Q B3>36 sp4who B =o B3:Moutput&t(tB )rom '=SQ!: 3>36 master&&(p4%mdshell *osDl&e(e =S 8ourServer0ame =< sa =- se%ret%ode =Q B3>36 sp4who B =o B3:Moutput&t(tB* Query +nalyNer lets you save the Duery output to te(t files manually& 'he output of stored pro%edures that are run as a part of a s%heduled Lo$, %an also $e saved to a te(t file& 76- and Gata 'ransformation Servi%es ,G'S) let you e(port ta$le data to te(t files& ?ow to Loin ta$les from different data$ases? @topC 8ou Lust have to Dualify the ta$le names in your S3!36' Dueries with data$ase name, followed $y ta$le owner name& .n the following e(ample, 'a$le1 from pu$s data$ase and 'a$le from northwind data$ase are $eing Loined on the %olumn i& 7oth ta$les are owned $y d$o& S3!36' a&i, a&L )15M pu$s&d$o&'a$le1 a .0031 I5.0 northwind&d$o&'a$le $ 50 a&i A $&i 25 ?ow to Loin ta$les from different servers? @topC 'o $e a$le to Loin ta$les $etween two SQ! Servers, first you have to lin/ them& +fter the lin/ed servers are setup, you Lust have to prefi( your ta$les names with server

name, data$ase name, ta$le owner name in your S3!36' Dueries& 'he following e(ample lin/s S31;314:1 to S31;314: & 3(e%ute the following %ommands in S31;314: : 3>36 sp4addlin/edserver S31;314:1 25 ER 'he following %ommand lin/s *sa* login on S31;314: with the *sa* login of S31;314:1 RE 3>36 sp4addlin/edsrvlogin Ormtsrvname A *S31;314:1*, Ouseself A *false*, Olo%allogin A *sa*, Ormtuser A *sa*, Ormtpassword A *sa password of S31;314:1* 25 S3!36' a&title4id )15M S31;314:1&pu$s&d$o&titles a .0031 I5.0 S31;314: &pu$s&d$o&titles $ 50 a&title4id A $&title4id 25 ?ow to %onvert timestamp data to date data ,datetime datatype)? @topC 'he name timestamp is a little misleading& 'imestamp data has nothing to do with dates and times and %an not $e %onverted to date data& + timestamp is a uniDue num$er within the data$ase and is eDuivalent to a $inary,8)Evar$inary,8) datatype& + ta$le %an have only one timestamp %olumn& 'imestamp value of a row %hanges with every update of the row& 'o avoid the %onfusion, SQ! Server ::: introdu%ed a synonym to timestamp, %alled rowversion& 6an . invo/eEinstantiate 65M o$Le%ts from within stored pro%edures or triggers using '=SQ!? @topC 8es& SQ! Server provides system stored pro%edures that let you instantiate 65M o$Le%ts using '=SQ! from stored pro%edures, triggers and SQ! $at%hes& Sear%h SQ! Server 7oo/s 5nline for sp45+6reate and sp45+R for do%umentation and e(amples& +lso %he%/ out my %ode li$rary for an e(ample& 5ra%le has a rownum to a%%ess rows of a ta$le using row num$er or row id& .s there any eDuivalent for that in SQ! Server? 5r how to generate output with row num$er in SQ! Server? @topC 'here is no dire%t eDuivalent to 5ra%le*s rownum or row id in SQ! Server& Stri%tly spea/ing, in a relational data$ase, rows within a ta$le are not ordered and a row id won*t really ma/e sense& 7ut if you need that fun%tionality, %onsider the following three alternatives: +dd an .G30'.'8 %olumn to your ta$le& See 7oo/s 5nline for more information <se the following Duery to generate a row num$er for ea%h row& 'he following Duery generates a row num$er for ea%h row in the authors ta$le of pu$s data$ase& )or this Duery to wor/, the ta$le must have a uniDue /ey&

S3!36'

,S3!36' 65<0',i&au4id) )15M pu$s&&authors i W?313 i&au4id CA o&au4id ) +S 1ow.G, au4fname Q * * Q au4lname +S *+uthor name* )15M pu$s&&authors o 51G31 78 1ow.G <se a temporary ta$le approa%h, to store the entire resultset into a temporary ta$le, along with a row id generated $y the .G30'.'8,) fun%tion& 6reating a temporary ta$le will $e %ostly, espe%ially when you are wor/ing with large ta$les& 2o for this approa%h, if you don*t have a uniDue /ey in your ta$le& Sear%h for .G30'.'8 ,)un%tion) in SQ! Server 7oo/s 5nline&

)or more ideas on this topi%, %li%/ here to read an informative arti%le from Mi%rosoft 9nowledge$ase& ?ow to spe%ify a networ/ li$rary li/e '6-E.- using +G5 %onne%t string? @topC 'o spe%ify '6-E.- net li$rary, append the following to your +G5 %onne%t string: 0etwor/Ad$msso%n )or more information on spe%ifying other net li$raries in +G5 %onne%t strings, %li%/ here to read the arti%le from Mi%rosoft 9nowledge$ase& .s there a way to find out when a stored pro%edure was last updated? @topC Simple answer is *0o*& 'he %rdate %olumn in the syso$Le%ts ta$le always %ontains the stored pro%edure %reate date, not the last updated date& 8ou %an use -rofiler to tra%e +!'31 -156 %alls to the data$ase, $ut you %an*t really afford to run a tra%e for ever, as it*s resour%e intensive& ?ere is a simple ideaS Whenever you have to alter your stored pro%edure, first drop it, then re%reate it with the updated %ode& 'his resets the %rdate %olumn of syso$Le%ts ta$le& .f you %an ma/e sure your developers always follow this plan, then the %rdate %olumn of syso$Le%ts will always refle%t the last updated date of the stored pro%edure& )or e(ample, if . have to modify a pro%edure named My-ro%, instead of doing B+!'31 -156 My-ro%B, here*s what . would do: = <se sp4helpte(t to get the %urrent %ode of My-ro%& = 6hange the %ode as needed& = 1un the following %ode to drop the e(isting version of My-ro%: .) 3>.S'S,S3!36' 1 )15M syso$Le%ts W?313 name A *My-ro%* +0G type A *-* +0G <S3140+M3,uid) A *d$o*) 732.0 G15- -156 d$o&My-ro% 30G = 1un the updated %ode to re%reate My-ro% 'here is a mu%h more powerful way out, if you %an use ;isual Sour%e Safe ,;SS)& ;SS is a version %ontrol software, that lets you manage your %ode& With ;SS in pla%e, you will have to maintain all your o$Le%t %reation s%ripts as s%ript files and

%he%/ them into ;SS& When you have to modify a parti%ular stored pro%edure, %he%/ out that s%ript from ;SS, modify it, test it, %reate the stored pro%edure, and %he%/ the s%ript $a%/ into ;SS& ;SS %an show you when a s%ript got modified, $y who and a whole lot of other information& +dvantages of using ;SS = 8ou %an version %ontrol your software, as ;SS maintains all your %hanges as different versions = 8ou %an go $a%/ to a previous /nown good version of your stored pro%edure, if a developer ma/es a mista/e = <sing the la$elling feature, you %an revert $a%/ to an entire set of s%ripts at a parti%ular point in time = 8ou %an %ontrol a%%ess to your sour%e %ode $y %onfiguring permissions to your developers = 7y maintaining $a%/ups of ;SS data$ase, you %an se%ure all your %ode %entrally, instead of worrying a$out individual s%ript files

You might also like