sql-server-2008-r2

Programmatically copy indexes from one table to another in SQL Server

你说的曾经没有我的故事 提交于 2019-12-02 20:58:00
Basically the exact same question as in this question: How to copy indexes from one table to another in SQL Server , BUT, how do I do it programmatically in T-SQL, given a source table name and destination table name? I.e. without knowing what table up front. I can copy the basic structure SELECT TOP (0) * INTO [BackupTable] FROM [OriginalTable] But that doesn't copy indexes, constraints, triggers etc I ideally would like a stored proc that looks something like: spCloneTableStructure @ExistingTableName, @NewTableName That copies the columns, primary keys and indexes Anything like that exist?

How to SELECT and UNION from a group of Tables in the schema in SQL Server 2008 R2

强颜欢笑 提交于 2019-12-02 20:51:22
问题 I have a group of tables in my SQL Server 2008 R2 database, all of them with the same columns. I need to SELECT and UNION these tables. I can SELECT the tables I want like this: SELECT TABLE_NAME FROM information_schema.tables where (TABLE_NAME like '%_CH1' or TABLE_NAME like '%_CH0') and TABLE_NAME not like '%test%' What I need now is to SELECT Column1, Column2, Column3 from the first guy in that list, UNION SELECT Column1, Column2, Column3 from the next guy in that list and so forth. How do

Control on XML elements nesting using FOR XML

有些话、适合烂在心里 提交于 2019-12-02 20:31:44
问题 I state my problem by example. In fact i foudna solution after trying in many ways but i would like to ask whether this solution is good or if for any reason it is better to use an alternative approach. In fact i need to control how elements are created. i first made a view containing all the data i needed and then i selected from teh view by joining the view more times. I reproduced the "complexity" here using a local variable instead of a view: DECLARE @Employees table( EmpID int NOT NULL,

Stored procedure without cursors

空扰寡人 提交于 2019-12-02 20:14:02
问题 How can I write the following sp without the cursor?. More over its not giving me the desired output. I didn't write this, I am trying to interpret what is wrong with this. ALTER PROCEDURE [dbo].[AccreditationExpiryCheck] AS BEGIN SET NOCOUNT ON; declare @taskTypeId int = 19 -- Accreditations, automated declare @firstActionTypeId int = 23 -- Accreditation expiring declare @nextActionTypeId int = 3 -- Call company declare @companyId int declare @accreditationId int declare @comment nvarchar

In SQL Server 2008 R2 script data missing on Script Wizard

半腔热情 提交于 2019-12-02 20:11:34
In my SQL Server 2008 R2 Script Option Screen of Script Wizard under section Table/View Options Look I find Script Data row and want to turn the option to True but I fail. I don't find any script data option. Why script data option missing on my SQL Server 2008 R2? Is there any command to generate the insert data option. Why this problem arise? how to solve it? Thanks in advance shamim It looks like they removed it from that location. You can still do it by: Right clicking on the DB Click on tasks Click on generate scripts Go through the wizard and select your tables On the options page click

Query to get the next identity? [closed]

為{幸葍}努か 提交于 2019-12-02 20:06:07
问题 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 6 years ago . Query to get the next identity? This is possible for table without deleted records: SELECT TOP 1 EMPID + 1 FROM Employee ORDER BY EMPID DESC If there is deleted data, How I will get the next identity ? For example I have a table like this: EMPID NAME 4001 someName 4002 someName 4003 ----------------------- this

SQL Server procedure declare a list

女生的网名这么多〃 提交于 2019-12-02 19:57:41
My SQL code is fairly simple. I'm trying to select some data from a database like this: SELECT * FROM DBTable WHERE id IN (1,2,5,7,10) I want to know how to declare the list before the select (in a variable, list, array, or something) and inside the select only use the variable name, something like this: VAR myList = "(1,2,5,7,10)" SELECT * FROM DBTable WHERE id IN myList You could declare a variable as a temporary table like this: declare @myList table (Id int) Which means you can use the insert statement to populate it with values: insert into @myList values (1), (2), (5), (7), (10) Then

Find records that do not have a corresponding record in the same table

两盒软妹~` 提交于 2019-12-02 19:37:29
问题 I have the following table: ID | JobID | Data | ResultType --------------------------------- 1 | 12345 | XXXX | 0 2 | 12345 | YYYY | 1 3 | 23456 | AAAA | 0 4 | 23456 | BBBB | 1 5 | 34567 | FOOB | 0 6 | 45678 | BARB | 0 Now I need to build a query that delivers all JobIDs where there is no entry with a ResultType = 1 EDIT1: So at the end, I want to result that delivers only JobIDs 34567 and 45678, because there is no record with a ResultType = 1 for those JobIDs . Can someone point me in the

Hash encrypting password when inserting into database

吃可爱长大的小学妹 提交于 2019-12-02 19:24:43
问题 I'm doing an application for school and I'm in need of help in encrypting passwords when inserting them into my users database.I'm programming in c# programming language and i'm using MS server 2008 R2 for manipulating my database. I'm thinking of doing a HASH encryption and I would love if someone helped me out. Here's my code for inserting data into database : using (SqlConnection con = new SqlConnection("Data Source=HRC0;Initial Catalog=users;Integrated Security=True")) //MLHIDE using

How to rename database in multi-user mode

最后都变了- 提交于 2019-12-02 19:24:09
I am working on SQL SERVER 2008 & 2008 R2. How can I rename a database in multi-user mode? I am using sp_rename but it returns this error: Msg 15225, Level 11, State 1, Procedure sp_rename, Line 338 You can't rename a database while it is in use. Either wait for a maintenance window, or force the database to single user mode (which will kick everyone out): USE [master]; GO ALTER DATABASE foo SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO --EXEC sys.sp_renamedb @dbname = N'foo', @newname = N'bar'; ALTER DATABASE foo MODIFY NAME = bar; -- preferred way GO ALTER DATABASE bar SET MULTI_USER; You can