sql-server-2005

How to split date ranges based on months in SQL Server 2005

风格不统一 提交于 2019-12-19 04:41:27
问题 Ex: date range is 01-01-2011 to 01-01-2012 , I want the output like this : 01-01-2011 31-01-2011 01-02-2011 28-02-2011 How can I do this ? I'm using SQL Server 2005.. Thanks 回答1: Using CTE: DECLARE @Begin DATETIME DECLARE @End DATETIME SELECT @Begin = '20110101', @End = '20120101' ;WITH ranges(DateFrom, DateTo) AS ( SELECT @Begin, DATEADD(DAY, -1, DATEADD(MONTH, 1, @begin)) UNION ALL SELECT DATEADD(month, 1, DateFrom), DATEADD(DAY, -1, DATEADD(MONTH, 2, DateFrom)) FROM ranges WHERE DateFrom <

Is it possible to password protect an SQL server database even from administrators of the server?

寵の児 提交于 2019-12-19 04:31:51
问题 I want to install an application (ASP.Net + SQL server 2005 express) in local network of some small company for demoing it for a period of time, but I also want nobody even sysadmin have no permission on this database and any permission granting wants a secure pass that I have . I just want my tables structure and relations and functions be hidden and encrypting the data have no advantage I need to spend more time on this article Database Encryption in SQL Server 2008 Enterprise Edition that

Convert a number to letters

耗尽温柔 提交于 2019-12-19 04:12:52
问题 Given a number, how do you convert that to letters? 回答1: You can just translate your existing logic into a t-sql scalar function, like so: CREATE FUNCTION dbo.fnColumnNameFromIndex(@i int) RETURNS varchar(3) AS BEGIN DECLARE @dividend int, @letters varchar(3), @modulo int SET @dividend = @i SET @letters = '' WHILE @dividend > 0 BEGIN SET @modulo = (@dividend - 1) % 26 SET @letters = CHAR(65 + @modulo) + @letters SET @dividend = CONVERT(int, (@dividend - @modulo) / 26 ) END RETURN @letters;

Known issue?: SQL Server 2005 stored procedure fails to complete with a parameter

丶灬走出姿态 提交于 2019-12-19 03:46:09
问题 Your basic SP with a default parameter: ALTER PROCEDURE [usp_debug_fails] @DATA_DT_ID AS int = 20081130 WITH RECOMPILE AS BEGIN /* Usage: EXEC [usp_debug_fails] WITH RECOMPILE */ -- Stuff here that depends on DATA_DT_ID END The same SP with a local that is hardcoded. ALTER PROCEDURE usp_debug_works] WITH RECOMPILE AS BEGIN /* Usage: EXEC [usp_debug_works] WITH RECOMPILE */ DECLARE @DATA_DT_ID AS int SET @DATA_DT_ID = 20081130 -- Stuff here that depends on DATA_DT_ID END You can see where I

Known issue?: SQL Server 2005 stored procedure fails to complete with a parameter

走远了吗. 提交于 2019-12-19 03:46:01
问题 Your basic SP with a default parameter: ALTER PROCEDURE [usp_debug_fails] @DATA_DT_ID AS int = 20081130 WITH RECOMPILE AS BEGIN /* Usage: EXEC [usp_debug_fails] WITH RECOMPILE */ -- Stuff here that depends on DATA_DT_ID END The same SP with a local that is hardcoded. ALTER PROCEDURE usp_debug_works] WITH RECOMPILE AS BEGIN /* Usage: EXEC [usp_debug_works] WITH RECOMPILE */ DECLARE @DATA_DT_ID AS int SET @DATA_DT_ID = 20081130 -- Stuff here that depends on DATA_DT_ID END You can see where I

Possible to implement a manual increment with just simple SQL INSERT?

Deadly 提交于 2019-12-19 03:17:00
问题 I have a primary key that I don't want to auto increment (for various reasons) and so I'm looking for a way to simply increment that field when I INSERT. By simply, I mean without stored procedures and without triggers, so just a series of SQL commands (preferably one command). Here is what I have tried thus far: BEGIN TRAN INSERT INTO Table1(id, data_field) VALUES ( (SELECT (MAX(id) + 1) FROM Table1), '[blob of data]'); COMMIT TRAN; * Data abstracted to use generic names and identifiers

How to make awkward pivot of sql table in SQL Server 2005?

假如想象 提交于 2019-12-19 02:39:50
问题 I have to rotate a given table from an SQL Server but a normal pivot just doesn't work (as far as i tried). So has anybody an idea how to rotate the table into the desired format? Just to make the problem more complicated, the list of given labels can vary and it is possible that a new label name can come into at any given time. Given Data ID | Label | Numerator | Denominator | Ratio ---+-----------------+-------------+---------------+-------- 1 | LabelNameOne | 41 | 10 | 4,1 1 | LabelNameTwo

Firing trigger for bulk insert

纵饮孤独 提交于 2019-12-19 02:26:48
问题 ALTER TRIGGER [dbo].[TR_O_SALESMAN_INS] ON [dbo].[O_SALESMAN] AFTER INSERT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for trigger here DECLARE @SLSMAN_CD NVARCHAR(20) DECLARE @SLSMAN_NAME NVARCHAR(20) SELECT @SLSMAN_CD = SLSMAN_CD,@SLSMAN_NAME=SLSMAN_NAME FROM INSERTED IF NOT EXISTS(SELECT * FROM O_SALESMAN_USER WHERE SLSMAN_CD = @SLSMAN_CD) BEGIN INSERT INTO O_SALESMAN_USER(SLSMAN_CD, PASSWORD

SQL 2005 Can I use keyword like in a case statement

℡╲_俬逩灬. 提交于 2019-12-18 18:58:27
问题 I need a case statement that allows partial matches. I get a syntax error, but am wondering if anything similar to this is possible. If not, my secondary solution is to re-write as a cursor... Example: SELECT CASE ColumnName WHEN 'value1' THEN 'answer1' WHEN 'value2' THEN 'answer2' WHEN LIKE '%TEST%' THEN 'answer3' END AS Answer FROM TableName 回答1: try this SELECT CASE WHEN ColumnName = 'value1' THEN 'answer1' WHEN ColumnName = 'value2' THEN 'answer2' WHEN ColumnName LIKE '%TEST%' THEN

ExecuteReader: Connection property has not been initialized

喜夏-厌秋 提交于 2019-12-18 18:35:11
问题 ExecuteReader: Connection property has not been initialized. my coding is protected void Button2_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=Si-6\\SQLSERVER2005;Initial Catalog=rags;Integrated Security=SSPI"); SqlDataReader rdr = null; try { // 2. Open the connection conn.Open(); // 3. Pass the connection to a command object //SqlCommand cmd = new SqlCommand("select * from Customers", conn); SqlCommand cmd=new SqlCommand ("insert into time(project