sql-server-2000

Is SQL IN bad for performance?

≯℡__Kan透↙ 提交于 2019-11-26 03:53:08
问题 I have a query doing something like: SELECT FieldX, FieldY FROM A WHERE FieldW IN (108, 109, 113, 138, 146, 160, 307, 314, 370, 371, 441, 454 ,457, 458, 479, 480, 485, 488, 490, 492, 519, 523, 525, 534, 539, 543, 546, 547, 550, 564, 573, 629, 642, 643, 649, 650, 651, 694, 698, 699, 761, 762, 768, 772, 773, 774, 775, 778, 784, 843, 844, 848, 851, 852, 853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863, 864, 865, 868, 869, 871, 872, 873, 891) Having an IN clause with so many options, is it

Add a column with a default value to an existing table in SQL Server

淺唱寂寞╮ 提交于 2019-11-26 03:16:23
问题 How can a column with a default value be added to an existing table in SQL Server 2000 / SQL Server 2005? 回答1: Syntax: ALTER TABLE {TABLENAME} ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE} WITH VALUES Example: ALTER TABLE SomeTable ADD SomeCol Bit NULL --Or NOT NULL. CONSTRAINT D_SomeTable_SomeCol --When Omitted a Default-Constraint Name is autogenerated. DEFAULT (0)--Optional Default-Constraint. WITH VALUES --Add if Column is Nullable and you

Check if table exists in SQL Server

一世执手 提交于 2019-11-26 03:14:15
问题 I would like this to be the ultimate discussion on how to check if a table exists in SQL Server 2000/2005 using SQL Statements. When you Google for the answer, you get so many different answers. Is there an official/backward and forward compatible way of doing it? Here are two possible ways of doing it. Which one among the two is the standard/best way of doing it? First way: IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE=\'BASE TABLE\' AND TABLE_NAME=\'mytablename\')

SQL Server: How to Join to first row

陌路散爱 提交于 2019-11-26 01:48:04
问题 I\'ll use a concrete, but hypothetical, example. Each Order normally has only one line item : Orders: OrderGUID OrderNumber ========= ============ {FFB2...} STL-7442-1 {3EC6...} MPT-9931-8A LineItems: LineItemGUID Order ID Quantity Description ============ ======== ======== ================================= {098FBE3...} 1 7 prefabulated amulite {1609B09...} 2 32 spurving bearing But occasionally there will be an order with two line items: LineItemID Order ID Quantity Description ========== ==

Can I protect against SQL injection by escaping single-quote and surrounding user input with single-quotes?

喜夏-厌秋 提交于 2019-11-26 00:23:41
问题 I realize that parameterized SQL queries is the optimal way to sanitize user input when building queries that contain user input, but I\'m wondering what is wrong with taking user input and escaping any single quotes and surrounding the whole string with single quotes. Here\'s the code: sSanitizedInput = \"\'\" & Replace(sInput, \"\'\", \"\'\'\") & \"\'\" Any single-quote the user enters is replaced with double single-quotes, which eliminates the users ability to end the string, so anything

How do I generate random number for each row in a TSQL Select?

穿精又带淫゛_ 提交于 2019-11-25 23:18:16
问题 I need a different random number for each row in my table. The following seemingly obvious code uses the same random value for each row. SELECT table_name, RAND() magic_number FROM information_schema.tables I\'d like to get an INT or a FLOAT out of this. The rest of the story is I\'m going to use this random number to create a random date offset from a known date, e.g. 1-14 days offset from a start date. This is for Microsoft SQL Server 2000. 回答1: Take a look at SQL Server - Set based random