sql-server-2000

t-sql replace on text field

孤人 提交于 2019-12-03 19:07:17
问题 I have hit a classic problem of needing to do a string replace on a text field in an sql 2000 database. This could either be an update over a whole column or a single field I'm not fussy. I have found a few examples of how to use updatetext to achieve it but they tend to be in stored procedures, does anyone know of a similar thing that is wrapped into a function so I can use it like I would usually use Replace(). The problem with the Replace() function for anyone who isn't aware is that it

SQL 2000: T-SQL to get foreign key relationships for a table

*爱你&永不变心* 提交于 2019-12-03 16:43:34
Similar but NOT IDENTICAL to SQL Server 2000 - Query a Table’s Foreign Key relationships I need a T-SQL statement that will work SQL 2000 that given a table name, will return the foreign key relationships for that table e.g. Table MyFristTable has a foreign key to MySecondTable, where MyFirstTable.ColA must be in MySecondTable.ColB. I'd be delighted, if the sql statement (or stored proc) is ran for MyFirstTable and returned a result set on the lines of Column | FK_Table | FK_COLUMN ---------------------------------- ColA | MySecondTable | ColB NB : I have samples for SQL 2005 that won't work

Inserting n number of records with T-SQL

一笑奈何 提交于 2019-12-03 16:29:24
问题 I want to add a variable number of records in a table (days) And I've seen a neat solution for this: SET @nRecords=DATEDIFF(d,'2009-01-01',getdate()) SET ROWCOUNT @nRecords INSERT int(identity,0,1) INTO #temp FROM sysobjects a,sysobjects b SET ROWCOUNT 0 But sadly that doesn't work in a UDF (because the #temp and the SET ROWCOUNT). Any idea how this could be achieved? At the moment I'm doing it with a WHILE and a table variable, but in terms of performance it's not a good solution. 回答1: If

How to increment in a select query

Deadly 提交于 2019-12-03 12:59:53
I've got a query I'm working on and I want to increment one of the fields and restart the counter when a key value is different. I know this code doesn't work. Programmatically this is what I want... declare @counter int, @id set @counter = 0 set @id = 0 select distinct id, counter = when id = @id then @counter += 1 else @id = id @counter = 1 ...with the end result looking something like this: ID Counter 3 1 3 2 3 3 3 4 6 1 6 2 6 3 7 1 And yes, I am stuck with SQL2k. Otherwise that row_number() would work. Assuming a table: CREATE TABLE [SomeTable] ( [id] INTEGER, [order] INTEGER, PRIMARY KEY

SQL Server Deadlock Fix: Force join order, or automatically retry?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 12:59:39
i have a stored procedure that performs a join of TableB to TableA : SELECT <--- Nested <--- TableA Loop <-- | ---TableB At the same time, in a transaction, rows are inserted into TableA , and then into TableB . This situation is occasionally causing deadlocks, as the stored procedure select grabs rows from TableB , while the insert adds rows to TableA , and then each wants the other to let go of the other table: INSERT SELECT ========= ======== Lock A Lock B Insert A Select B Want B Want A ....deadlock... Logic requires the INSERT to first add rows to A , and then to B , while i personally

Start stored procedures sequentially or in parallel

こ雲淡風輕ζ 提交于 2019-12-03 07:32:32
问题 We have a stored procedure that runs nightly that in turn kicks off a number of other procedures. Some of those procedures could logically be run in parallel with some of the others. How can I indicate to SQL Server whether a procedure should be run in parallel or serial — ie: kicked off of asynchronously or blocking? What would be the implications of running them in parallel, keeping in mind that I've already determined that the processes won't be competing for table access or locks- just

T-SQL List Tables, Columns

雨燕双飞 提交于 2019-12-03 06:50:18
问题 In T-SQL (SQL Server 2000). How can I list all tables and columns in a database? Also, in a separate query is there a way to list all columns along with data type and constraints (NULLS, etc). Thanks. 回答1: Please check out the information schema. select * from MyDatabaseName.information_schema.columns order by table_name, ordinal_position 回答2: Many ways to do it. Below are couple of ways that you can list : Option 1: SELECT db_name() as DATABASE_NAME, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME,

How do I join the first row of a subquery?

浪尽此生 提交于 2019-12-03 06:47:51
问题 I've got a table of invoices and a child table of related data related by key. In particular, for each invoice, I'm interested in only the first related row from the child table. Given that I want the one related row for every invoice key - how do I accomplish this? Select i.[Invoice Number], c.[Carrier Name] From Invoice i Left Join Carriers c on i.[InvoiceKey] = c.[InvoiceKey] Where -- what? I guess semantically speaking, what I'm looking for something akin to the concept of Top 1 c

How can I get the ID of the last INSERTed row using PDO with SQL Server?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 06:33:57
Under other circumstances I might be tempted to use $result = mssql_query("INSERT INTO table (fields) VALUES (data); SELECT CAST(scope_identity() AS int)"); but as I will be inserting user-submitted data, I want to continue to use PDO, which returns an empty array. Unfortunately, I'm running PHP on a Linux server and using dblib to interface with Microsoft SQL Server, which doesn't support PDO::lastInsertID() . Please help! Update to include code example Here's the code I'm using: col1 is a field of type int identity and col2 is a datetime with a default of getdate() . // Connect to db with

Is there a “poor man's” alternative to RedGate for scripting out entire database schema? [closed]

核能气质少年 提交于 2019-12-03 05:02:37
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I'm in a situation where I would to generate a script for a database that I could run on another server and get a database identical to the original one, but without any of the data. In essence, I want to end up with a big create script that captures the database schema. I am working in an environment that has