Making Part Of A Table Name Into A Variable
Aug 23, 2007
I have a huge question, it's for my job im doing now. I have a table with the name TWO.dbo.SVC06105. I want to be able to take the word "TWO" and put that into a variable. I know this doesn't look correct, but I want to be able to do something like this:
DECLARE @GPNAME CHAR(100)
SET @GPNAME = 'TWO'
SELECT * FROM @GPNAME.dbo.SVC06105
I know the SELECT statement isn't correct, but I hope that you guys understand what i'm trying to do through this example. BTW, I am new to this team so nice to meet you all. L8er
View 4 Replies
ADVERTISEMENT
May 28, 2008
Hi,
I am using a local variable to capture datetime and then select records from another table by making use of the above local variable result. But the query is running too slow when I use the local variable
declare @a smalldatetime
select @a=last_run_time from job_status
where job_des='sample'
SELECT * FROM History
WHERE CHANGE_DATE> @a
Instead of the above select statement if I use the below statement it is returning results quickly. Can someone help me in tuning the above query.
SELECT * FROM History
WHERE CHANGE_DATE> '5/23/2008 6:22:00.000 AM '
History table has columns ( case number, change_date, change_desc). I have two indexes defined on the above table one is on case_number and the other on change_date.I tried using force index but still the query is running slow.
will there be any difference if use dynamic sql ?
View 7 Replies
View Related
Aug 3, 2007
I know this can be done, because I saw it and tried it once. But did not use it at the time and consequently forgot how.
I am storing an expression to a variable @Var Varchar(50)
@Var = ('aa','bb','cc')
I want to:
Select * from Table WHERE Field IN @Var
Substituting my variable for the " ('aa',bb','cc') which I can build dynamically...thus the use.
Purpose:
I have an aggregate query that currently has a CASE statement based on the contents of ('aa',bb','cc'). However, those contents can change. The aggregate will not allow the subquery (SELECT Field FROM Table).
What I remember looked something like:
SELECT * FROM Table WHERE field in ' & @Var &'.
View 1 Replies
View Related
Mar 9, 2008
hey all,
I have the following query:
ALTER PROCEDURE [dbo].[sp_SelectMostRecentArticle]
AS
BEGIN
DECLARE @article_id INT
SELECT @article_id = (
SELECT TOP 1 article_id
FROM article
ORDER BY article_id DESC
)
DECLARE @comment_count INT
SELECT @comment_count = (
SELECT COUNT(comment_id)
FROM comment
JOIN article ON article_id = comment_article_id
GROUP BY article_id
HAVING article_id = @article_id
)
SELECT TOP 1 article_id, article_author_id,
article_title, article_body, article_post_date,
article_edit_date, article_status, article_author_id
article_author_ip, author_display_name,
category_id, category_name--, comment_count AS @comment_count
FROM article
JOIN author ON author_id = article_author_id
JOIN category ON category_id = article_category_id
GROUP BY article_id, article_title, article_body, article_post_date,
article_edit_date, article_status, article_author_ip,article_author_id,
author_display_name, category_id, category_name
HAVING article_id = @article_id
END
GO
as you can see, im trying to return a comment_count value, but the only way I can do this is by defining the variable.
I have had to do it this way, because I cannot say COUNT(comment.comment_id) AS comment_count or it returns an error that it cant reference the comment.comment_id.
But when change it to FROM article, comment; I get errors about the article_author_id and article_comment_id.
And i cant add a join, because it would return the amount of rows of the comment...
unless someone could help with what i Just decribed (as i would prefer to do it this way), how would i return the variable value as part of the select statement?
Cheers
View 6 Replies
View Related
Nov 27, 2007
Hi,
I had problem when combining OpenRowSet and SP_EXECUTESQL, when i tried to run the following query, it complaints that RESID is not declared. any idea how should i put the query so i will pass @RESID as 1 of the parameter? BTW, i know that the SP_EXECUTESQL is able to run query which length up to 8000, but how about the parameter?
DECLARE @SqlToRun NVARCHAR(MAX)
DECLARE @RESOURCEIDS NVARCHAR(MAX)
SET @RESOURCEIDS = REPLICATE(CAST('ABC' AS NVARCHAR(MAX)), 1000)
SET @SqlToRun = N'SELECT * FROM
OPENROWSET(''SQLNCLI'',
''server=;database=;uid=;pwd='',
''SELECT * FROM WHERE COL=@RESID'')'
DECLARE @PARAMS NVARCHAR(MAX)
SET @PARAMS = ''@RESID NVARCHAR(MAX)'
EXECUTE SP_EXECUTESQL @SqlToRun, @PARAMS, @RESID = @RESOURCEIDS
Regards,
Derek
View 5 Replies
View Related
Jan 31, 2007
hi. i'm trying to make a report in asp that sums up a whole bunch of statistics. the report is quite lengthy and takes about 2 minutes to load. i thought i would solve that problem by making a table to dump the statistics into instead of calculating the statistics every time the report is viewed. this way, the page would just read the table with the statistics already put in it. these statistics would be updated every day or two so i don't need anything up to the minute.however, i have run into a small problem which is actually updating the information in the table. my new table is called OfficeReport and it looks like this:UserID Stat1 Stat2 Stat3 ...... Stat3223 0 0 0 056 0 0 0 072 0 0 0 0this is a query for one of the statistics i am currently using:SELECT DefendantCase.UserID, COUNT(DefendantCase.UserID) AS CountOfUserID FROM DefendantCase LEFT JOIN UserDescription ON DefendantCase.UserID=UserDescription.UserID WHERE UserDescription.Status=1 AND UserDescription.UserType=1GROUP BY DefendantCase.UserIDif i run this query, this is what i get:UserID CountOfUserID54 2160 1052 294 427 22how can i modify this query so its output updates the same UserID column in the OfficeReport table? Thanks!
View 3 Replies
View Related
Feb 10, 2004
I have a Query named qryQuery1 from which I would like to make a Table named tblTable1. I need to do this within a VBA Microsoft Access module.
Can anyone help me with the proper code?
Thank you for any help.
Jim
View 14 Replies
View Related
Apr 3, 2006
I need to make a backup of a table so that if I mess it up I can put the data back to where I started...
I am going to insert some new rows and update values in some existing rows. So before I do this and find out I messed it up...how do I make a copy of Table A as Table B?
I would actually like to make a copy to table B, then run my changes into B, make sure all is correct, and then transfer the revised data from B over to table A replacing its data. What approach would be best here to accomplish this?
Thanks
View 2 Replies
View Related
Sep 24, 2004
Hi Guys,
I have a requirement as follows:->
I have a table like below
EMPLOYEEID------- OWNS
********** ****
1-----------------car
1-----------------house
1-----------------dog
2-----------------house
3-----------------car
3-----------------bus
3-----------------shop
3-----------------hotel
3-----------------theater
3-----------------casino
Requirement:
I wanted to create another table based on the column values. For eg: I have to take the employee id and check for what value he has under owns column in the table. I take only 3 values and then these values should go to the newly created columns (owns1, owns2,owns3).
if there is no value for any of these columns it should have null values loaded in them.
The result of the modification should look like this:->
EMPLOYEEID-------OWNS1------OWNS2------OWNS3
*********** ***** ***** *
1-----------------car--------house------dog
2-----------------house------Null-------Null
3-----------------car--------Bus--------shop
Note: eventhough employeeid 3 owns more than 3 things we only take 3 of what he owns and populate to above coloumns.
In addition to it, the column OWNS will have more than 500 different values in them.
Its kind of urgent and if anyone knows how to , Can you please help me on this.
Thanks a lot.
-- Ragulan
;)
View 3 Replies
View Related
Jan 8, 2015
How can I count the rows when I make new table. Here is my code:
SELECT EMPLOYEE_ID, LAST_NAME, SALARY, JOB_ID, COMMISSION_PCT, DEPARTMENT_ID, COUNT(TO_CHAR(HIRE_DATE, 'DAY')) AS NUM_EMPLOYEES
FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE, 'Day') = 'Monday'
GROUP BY EMPLOYEE_ID, LAST_NAME, SALARY, JOB_ID, COMMISSION_PCT, DEPARTMENT_ID;
Here is the table: [URL] ....
I have 10 rows, so I would like under NUM_EMPLOYEES to be displayed 10 on each row.
View 5 Replies
View Related
Nov 19, 2007
Hello, Im a very new to SQL server etc, so please bear with me.
I am currently trying to find a quick way of making a large number of database entries within a particular table all have the same value in one particular column.
I have created a query in MS SQL Server Management Studio, which outputs all the entries in a particular table that I want to change. Currently they all have different values in a certain column in this table, and I want them all to have the same value in that column.
How do I do this within Server Management Studio?
Thank you.
View 3 Replies
View Related
Apr 15, 2008
Hi.
I found unpleasant issue within RS:
1. Imagine RS report with Table(with interactive sorting) whose visibility is toggled by Textbox:
2. After sorting the Table the Textbox is getting collapsed making the Table invisible. But the Table should remain visible.
Is this MS bug? Are there workarounds?
If my question is not understandable I can send simple *.rdl report with sample.
Thank you in advance for help.
View 7 Replies
View Related
May 11, 2008
Hello,
I've seem some good posts similair to this, but I haven't been able to find my exact issue.
I have the following table:
ID
Name
Location
Start
End
1
Joe
NY
2000
2001
1
Joe
CA
2002
2004
1
Joe
MA
2005
2008
2
Sue
NJ
2003
2004
2
Sue
FL
2004
2008
3
Bob
CA
1999
2000
3
Bob
WA
2001
2004
3
Bob
OR
2005
2006
3
Bob
MI
2007
2008
As you can see, the Location, Start and End dates can vary for each person and I don't know how many rows a single person might have.
The result I want, is a "pivot like" table.
ID
Name
Location1
Start1
End1
Location2
Start2
End2
Location3
Start3
End3
Location4
Start4
End4
1
Joe
NY
2000
2001
CA
2002
2004
MA
2005
2008
2
Sue
NJ
2003
2004
FL
2004
2008
3
Bob
CA
1999
2000
WA
2001
2004
OR
2005
2006
MI
2007
2008
I assume I can first do a count of the maximum rows for an individual and that is greatest number of columns I would need. But doing that and trying to figure out the rest has really stumped me.
Any thoughts, ideas and suggestions would be greatly appreciated.
Thank you.
-Gumbatman
View 11 Replies
View Related
Oct 11, 2006
Hello,
I have a table that is fairly large, and I need to make a change to one of the columns in the table. Namely I need to change the datatype and rename that column. When I try to save the updated table, I keep getting a timeout error that says.
'eligibility (dbo)' table
- Unable to create index 'PK_eligibility'.
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Any ideas on how to make the table change more efficient or change the timeout period. I need to keep the existing data in the table. I am using sql server managment studio(2005) connected to a sql server 2000 database.
Thanks!
View 7 Replies
View Related
Apr 16, 2008
I have a pivot table query I am running and wanted to find out if there was a way to pull in the dates like getdate() - 12 months, getdate() - 11 months, etc. instead of hard coding the dates.
Here is my query
SELECT Client, [4/1/2007 12:00:00 AM] AS Month1, [5/1/2007 12:00:00 AM] AS Month2, [6/1/2007 12:00:00 AM] AS Month3, [7/1/2007 12:00:00 AM] AS Month4,
[8/1/2007 12:00:00 AM] AS Month5, [9/1/2007 12:00:00 AM] AS Month6, [10/1/2007 12:00:00 AM] AS Month7, [11/1/2007 12:00:00 AM] AS Month8,
[12/1/2007 12:00:00 AM] AS Month9, [1/1/2008 12:00:00 AM] AS Month10, [2/1/2008 12:00:00 AM] AS Month11, [3/1/2008 12:00:00 AM] AS Month12,
[4/1/2008 12:00:00 AM] AS Month13, Engineer
FROM (SELECT Client, DollarsBilled, SlipDates, Engineer
FROM dbo.MonthlyClientBillables) p PIVOT (SUM(DollarsBilled) FOR SlipDates IN ([4/1/2007 12:00:00 AM], [5/1/2007 12:00:00 AM],
[6/1/2007 12:00:00 AM], [7/1/2007 12:00:00 AM], [8/1/2007 12:00:00 AM], [9/1/2007 12:00:00 AM], [10/1/2007 12:00:00 AM], [11/1/2007 12:00:00 AM],
[12/1/2007 12:00:00 AM], [1/1/2008 12:00:00 AM], [2/1/2008 12:00:00 AM], [3/1/2008 12:00:00 AM], [4/1/2008 12:00:00 AM])) AS pvt
View 31 Replies
View Related
Oct 29, 2007
I have SSRS 2005 SP2 configured to work in Sharepoint integration. Everything works fine except that I am not able to programmatically change any property of report viewer web part (instance) that I have added on on home page of my sharepoint site.
I can do same thing via sharepoint UI but not through program. When my programs runs it fetches all web parts been added on home page, then I need to iterate through each one and find report viewer web part.
While iterating, as soon as I arrive to report viewer web part it is named as "Error web part" with error message as
"Windows SharePoint Services cannot deserialize the Web Part. Check the format of the properties and try again"
If someone has a solution, please respond at your earlist.
Thanks
Shankar
View 1 Replies
View Related
Dec 7, 2007
I have a table with a column named measurement decimal(18,1). If the value is 2.0, I want the stored proc to return 2 but if the value is 2.5 I want the stored proc to return 2.5. So if the value after the decimal point is 0, I only want the stored proc to return the integer portion. Is there a sql function that I can use to determine what the fraction part of the decimal value is? In c#, I can use
dr["measurement "].ToString().Split(".".ToCharArray())[1] to see what the value after the decimal is.
View 3 Replies
View Related
Nov 4, 2015
CREATE TABLE #T(branchnumber VARCHAR(4000))
insert into #t(branchnumber) values (005)
insert into #t(branchnumber) values (090)
insert into #t(branchnumber) values (115)
insert into #t(branchnumber) values (210)
insert into #t(branchnumber) values (216)
[code]....
I have a parameter which should take multiple values into it and pass that to the code that i use. For, this i created a parameter and temporarily for testing i am passing some values into it.Using a dynamic SQL i am converting multiple values into multiple records as rows into another variable (called @QUERY). My question is, how to insert the values from variable into a table (table variable or temp table or CTE).OR Is there any way to parse the multiple values into a table. like if we pass multiple values into a parameter. those should go into a table as rows.
View 6 Replies
View Related
Jan 28, 2015
I have made the following Scalar-valued function:
CREATE FUNCTION [dbo].[TimeCalc]
(
@OriginalTime AS INTEGER
, @TenthsOrHundredths AS INTEGER -- code 2: 1/10, code 4: 1/100
)
RETURNS NVARCHAR(8)
[Code] ....
What it does is convert numbers to times
E.g.: 81230 gets divided by 10 (times in seconds: 8123). This 1 1 full minute, and the remainder = 2123 making it 1.21.23 mins)
So far so good (function works perfectly)
My question: sometimes times are in 1/100 (like above sample), sometimes in 1/10.
This means that, e.g. with a time like 3.23.40 the last zero must be deleted.
My thoughts are to use the results from the Return Case part, and as the code = 4: leave it as it is,
is the code 2 the use LEFT(... result Return Case ..., Len(..result Return Case.. - 1))
There are 5 codes: 0 1 2 3 and 4
View 9 Replies
View Related
Apr 28, 2000
I have used the Microsoft code below to create a link to SQL server from access. I am using DAO not ADODB. Someone said that I should use dblib instead of ODBC in the connection string below in order to get the connection to work.
Does anyone know the correct syntax for OLE DB connection string and can it be used with DAO? I am getting an error "ISAM path not found"?? Seems I have wrong connection string syntax?? Please advise.
Thanks again for your help.
-------------------------------------------------------------------------
Sub ClientServerX3()
Dim dbsCurrent as Database
Dim tdRoy as TableDef
Set dbsCurrent = CurrentDb
Set tdfRoy = dbsCurrent.CreateTableDef("Roy")
tdfRoy.Connect = "ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers"
***************IS THERE ANYWAY TO USE AN OLEDB (OR OTHER DSN-LESS)
***************CONNECTION HERE? How??
tdfRoy.SourceTableName = "Royalties"
dbsCurrent.TableDefs.Append tdfRoy
End Sub
View 1 Replies
View Related
Apr 20, 2005
Tell me please how can I make a foreing key constraint for a table using from another not all primary key or not all data. For example, I need to link a table "subject" with primary key "dscp_num, depart" using depart as a foreing key. But there is no table with such primary key, there is only table "codif_values" where selecting records by condition "codif_num = 1" gives the relation with necesary primary key to link. Also I need to link "subject" table as a primary key table with the table "ses_curr". But this table doesn't include "depart" field. It includes field "reg_num" that is a primary key in table "students". This table include field "stgroup" as a foreing key for table "groups". The last table includes "speciality" field that is a foreing key for table "specialities". And only this table includes "depart" field. Tell me please how I can make a foreing key constraint for table "ses_curr" by table "subject".
View 3 Replies
View Related
Apr 30, 2015
I want to add a new column in a table. In this column I want to include a Total Value. This Total Value exists already for dates after Sept 23, 2013. For dates before Sept 23, 2013 the total can be calculated using the following math:
Total = Total(t-1) - (Resource_Name1-Resource_Name1).
The Resource_Name column contains the Total field (after Sept 23) as well as the Resource_Name1 field. There is a third column called Direction which contains the values injection or withdrawal. The Resource_Name1 differes in value depending on whether it is injection or withdrawal. How do I create a new column with Totals for the full data set?
View 9 Replies
View Related
Jun 5, 2007
I want to hide a row n all pages except the first page , is there a way for doing this,
The haeder has totally 5 rows, But i want to hide only the 4th row, that too I want it in the first page and not the rest of the pages
View 3 Replies
View Related
Jul 14, 2015
I have a table like this :
reg.no |Â Â description |Â start_date |Â end_date |Â Â load_date
I want to join this table with itself on reg. no. But not all the rows in table must be joined.
But for example, rows with load_date 01-07-2015 to be joined with rows with load_date 02-07-2015. And the rest of the rows should not used in join (for example, rows having other load_dates)
Is this possible ? and how?
View 4 Replies
View Related
Aug 14, 2006
Hi,
I need to update just a portion of a column value in a table.
example,
Order_data = 'source=ABD00050&ordsrc=&ecode=ABD00001'
Order_data ='source=ABD00050&ecode=ABD00001'
for both of these values of Order_data I just need to update value of ecode..
Please help..
Thanks,
Reva
View 8 Replies
View Related
Apr 1, 2006
In MS SQL Server, I have the following tables with some data in it.create table table1 (column1 varchar(32),column2 int not null,column10 varchar(255),.....primary key (column1, column2),);create table table2 (column1 varchar(32),column2 int not null,column20 varchar(255) not null,....foreign key (column1, column2) references table1(column1, column2));Now, I need to change the all column types from varchar to nvarchar tosupport internationalized character set.I am able to do so, for columns column10 in table1 and column20 oftable2 without any problems by using command:"alter table table1 alter column column10 nvarchar(255);"But, when I try the similar thing for column1 of table1/table2, amgetting one error message saying like: "ALTER TABLE ALTER COLUMN failedbecause one or more objects access this column". I guess, this iscoming because of foreign key relationship between the tables.NOTE: While defining the table2, for foreign key I have not specifiedanything like "on update cascase" ...etc.How can I resolve this issue? Any suggestions/solutions are reallyhelpful to me. Thanks in advance.
View 2 Replies
View Related
Mar 27, 2006
Hi,
I want to join a temporary table with a table in my stored procedure :
CREATE PROCEDURE sp_DeltaabgleichDarlehen
AS
BEGIN
SELECT Grundeinstellungen.Dat_Initialisierung, Grundeinstellungen.Dat_Jahresbeginn
INTO #temp_IniDatum
FROM Grundeinstellungen
INSERT INTO DeltaDarlStammdaten (Datum, Vertragsnummer)
SELECT ImpDarlStammdaten.Datum, ImpDarlStammdaten.Vertragsnummer
FROM ImpDarlStammdaten
WHERE ImpDarlStammdaten.Datum=#temp_IniDatum.Dat_Jahresbeginn
END
i get the error :
The multi-part identifier "#temp_IniDatum.Dat_Jahresbeginn" could not be boundfor example
What is the problem? and how can i solve this?
thank you
Jupp
View 4 Replies
View Related
Jan 17, 2007
Hi all,
I found an article which described about Distributed query to Oracle table with four-part name.
http://support.microsoft.com/kb/294459/en-us
However, we still got the same error with MSSQL2000 sp4 and SQL2005. Any fix to this?
View 1 Replies
View Related
Mar 12, 2008
I have 2 tables (table1KKK, table2KKK), and want to run the same query on them by using parameter with the value "1" or "2".
Is it possible to use that parameter as a part of the queried table name?
Something like (only for demonstration €“ doesn€™t work):
Select *
From table +myParameter + kkk
Thanks in advance!
View 3 Replies
View Related
Jul 25, 2003
In my database I want to archive some rows based on the time stamp. I read that horizontal partitioning will do that. This partitioning will insert the rows from the table into some other Database - Tablename. In retrivng those rows back I need to issue separate queries and UNION the result set. Is this way is correct or there any better ways to do this? If it is I have another question. If I add a new column to the table I partitioned, what will happen to the table that I stored in other Database. Is that going to add this new column too?
If possible please give me a sample code or queries to partition the database.
Thank you.
View 1 Replies
View Related
Oct 7, 2015
I'm trying to replace a particular part of a row in a table with a new value.
The row is called "DataPath" and it has a lot of values like so:
mashOperationsComponent Data FilesSantec
I want to run a query to replace the mash with our DFS namespace share name companysharesDepartments but keep everything else past the mash part of the original row.
I'm currently running this query, it says it is altering 30,000 rows, but it doesn't look like it's doing anything at all..
UPDATE dbo.Part
SET DataPath = REPLACE(DataPath,'company.localsharesDepartments','mash')
WHERE DataPath like 'mash\%'
So for example, it would change the mash above to
company.localsharesDepartmentsOperationsComponent Data FilesSantec
View 2 Replies
View Related
May 17, 2007
Hi there,
We're going to use replication on our database. There are tables without any primary key.
Is a primary key necessary for replication?
I have also antoher question: We want to replicate the whole OLTP database (size 30 GB).
Is snapshot replication a suitable mechanisme for this? I thougt snapshot replication overwrites all of the data, not only changed data. Or should we use another replication method?
Thanks in advance,
Patrick de Jong
View 8 Replies
View Related
Aug 29, 2007
which is more efficient...which takes less memory...how is the memory allocation done for both the types.
View 1 Replies
View Related