sql-server-2005

Cannot insert NULL values into column 'USERNAME', table 'tempdb.dbo.#temptable error

扶醉桌前 提交于 2019-12-12 13:55:28
问题 I have a SP. while executing I am getting error as Cannot insert the value NULL into column 'USERNAME', table 'tempdb.dbo.#temptable__________________________________________________________________________________________________________0000000002FD'; column does not allow nulls. UPDATE fails. The statement has been terminated. Below is my SP ALTER PROCEDURE [dbo].[UserReportData] @As_ONDATE Datetime AS BEGIN DECLARE @REPORTDATE datetime DECLARE @USERNAME varchar(110) Select * INTO

Insert into temporary table query

China☆狼群 提交于 2019-12-12 13:47:40
问题 I'm looking to optimise a query that takes an excessive amount of time to run (around 1.5 hours), and through looking at the execution plan one of the biggest drains seems to be inserting the data from the main query into a temporary table. When I insert the data into a temporary table there is the following process : Process (cost %) Compute Scalar (0%) <-- Stream Aggregate (0%) <-- Sort(0%) <-- Table Spool (eager spool) (10%) Which makes sense, but what doesn't to me is that this appears 9

What permissions are required to query master..syslogins in SQL Server?

折月煮酒 提交于 2019-12-12 13:15:58
问题 I have readonly access to the master database however the following query only returns a subset of users. What permissions are needed to return the rest of the users? SELECT [name] FROM master.dbo.syslogins 回答1: What version of SQL Server are you on?? The syslogins catalog view has been deprecated with SQL Server 2005 - if you're on 2005 or up, you should use sys.server_principals and sys.sql_logins instead. When you check the MSDN Books Online topic for sys.server_principals, you'll see a

Escape Character in SQL

丶灬走出姿态 提交于 2019-12-12 13:12:05
问题 Please forgive me as I am a bit of an sql noob. I am trying to do an insert using the following but am having a problem with the apostrophes. I have a lots of records to insert but many have the same problem. Is there a way of escaping them? INSERT INTO [dbo].[tb_Chefs] ([ChefHotelID], [HotelID], [ChefID], [Position], [GroupID]) VALUES(N'b809a86e-f7f2-45b2-a240-0049f51509d7' ,N'14481', N'624', N'Chef d'atelier', N'331') GO Any help much appreciated. 回答1: 'Chef d'atelier' becomes 'Chef d'

Is there a difference SMO ServerConnection transaction methods versus using the SqlConnectionObject property?

筅森魡賤 提交于 2019-12-12 13:04:04
问题 I am using SMO to create databases and tables on a SQL Server. I want to do so in a transaction. Are both of these methods of doing so valid and equivalent: First method: Server server; //... server.ConnectionContext.BeginTransaction(); //... server.ConnectionContext.CommitTransaction(); Second method: Server server; // ... SqlConnection conn = server.ConnectionContext.SqlConnectionObject; SqlTransaction trans = conn.BeginTransaction(); // ... trans.Commit(); 回答1: The two are equivalent.

join two tables with different number of rows(in sql server)

我是研究僧i 提交于 2019-12-12 12:42:01
问题 I have two tables: A (col1,col2,col3,col4) B (col1,col2,col3,col4) Table A has 4 records(rows) and B has 6 rows.I want to join them like this,for example join them in C table C ( B.col1 , B.col2 ,A.col3,A.col4,B.col3,B.col4) (tables have different values in records just col1 and col2 contains the same values) when i join them on A.col1=B.col1 and A.col2=B.col2 I take Cartesian product :( P.S I want to have 6 rows in C ,where B.col1,B.col2,B.col3,B.col4 have 6rows and A.col3,A.col4 have 4 rows

Insert Query: Why is it a bad idea to not include column names?

陌路散爱 提交于 2019-12-12 12:28:55
问题 I was told at my last position never to do this; it wasn't explained why. I understand it enhances the opportunity for mistakes, but if there are just a couple of columns and I'm confident of their order, why can't I shorthand it and just insert the values in the right order without explicitly matching up the column names? Is there a large performance difference? If so, does it matter on a small scale? If there's no performance hit and this isn't a query that will be saved for others to view,

How to throw exception from SQL server 2005 function?

北城余情 提交于 2019-12-12 12:27:16
问题 When I try to use RaiseError, I get the following compilation issue Msg 443, Level 16, State 14, Procedure ConvertSessionToCurr, Line 19 Invalid use of a side-effecting operator 'RAISERROR' within a function. So how do we handle exception in a function/ Change it to a out param stored proc? That really sounds like a bummer! 回答1: Options: Return NULL or some sentinel value Use a stored procedure Make it inline code My thought is that if you need RAISERROR in a UDF you're using it wrong... 来源:

A script to test existence of primary keys

允我心安 提交于 2019-12-12 12:08:26
问题 Trying to figure out a SQL script to test the existence of primary keys in cerain tables. If the table has no primary key, then the script should output the table name. Tables to test: TableA TableB TableC After running the script (and lets say TableA and TableC have PKs, but not TableB), then the output would be below: NoKeys TableB 回答1: ;WITH tables_with_pk AS ( SELECT t.table_schema, t.table_name FROM INFORMATION_SCHEMA.TABLES t INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc ON t.TABLE

SQL Server Arithmetic overflow error converting expression to data type datetime

风流意气都作罢 提交于 2019-12-12 12:06:15
问题 I want to customize my order by , if the Qty is negative then that row must be in fist then if the Qty is positive I will sort it by ExpDate SELECT WhsCode, ItemCode, LotNumber, ExpDate, Qty FROM rq_Test2 ORDER BY CASE WHEN qty < 0 THEN Qty ELSE ExpDate END But I am getting an error of " Arithmetic overflow error converting expression to data type datetime. " .. Why ? Thanks .. 回答1: Have two case statements Select WhsCode, ItemCode, LotNumber, ExpDate, Qty from rq_Test2 order by case when qty